private void ReadTOC(Stream S, TOC T) { ArrayList Entries = new ArrayList(); ArrayList SubTOCs = new ArrayList(); long BaseOffset = S.Position; TOCEntry TE = new TOCEntry(S, BaseOffset); bool ValidEntry = true; while (ValidEntry) { switch (TE.Type) { case EntryType.SubTOC: long Pos = S.Position; S.Seek(BaseOffset + TE.Offset_, SeekOrigin.Begin); TOC SubTOC = new TOC(); SubTOC.Name = TE.Name; this.ReadTOC(S, SubTOC); SubTOCs.Add(SubTOC); S.Seek(Pos, SeekOrigin.Begin); break; case EntryType.PNG: Entries.Add(TE); break; default: ValidEntry = false; break; } TE = new TOCEntry(S, BaseOffset); } T.SubTOCs = (TOC[])SubTOCs.ToArray(typeof (TOC)); T.Entries = (TOCEntry[])Entries.ToArray(typeof (TOCEntry)); }
private string GetName(TOCEntry entry) { string name; if (_rpfFile.Header.Identifier < HeaderIDs.Version3 || _rpfFile.Header.Identifier == HeaderIDs.Version4) { name = _rpfFile.TOC.GetName(Convert.ToInt32(entry.NameOffset)); } else { if (entry == _rpfFile.TOC[0]) { name = "Root"; } else if (_knownFilenames.ContainsKey((uint)entry.NameOffset)) { name = _knownFilenames[(uint)entry.NameOffset]; } else { name = string.Format("0x{0:x}", entry.NameOffset); } } return(name); }
private string GetName(TOCEntry entry) { string name; if (_rpfFile.Header.Identifier < MagicId.Version3) { name = _rpfFile.TOC.GetName(entry.NameOffset); } else { if (entry == _rpfFile.TOC[0]) { name = "/"; } else if (_knownFilenames.ContainsKey((uint)entry.NameOffset)) { name = _knownFilenames[(uint)entry.NameOffset]; } else { name = string.Format("0x{0:x}", entry.NameOffset); } if (AssumeVer3FilesAsAudio && entry is FileEntry) { name += "." + AudioFilesExtension; } } return(name); }
public PSARC() { EndianReader br = new EndianReader(new FileStream(@"C:\Users\diwidog\Documents\Development Items\PC\pak23.psarc", FileMode.Open, FileAccess.Read), EndianType.BigEndian); Header psHeader = new Header(); List <TOCEntry> TOCs = new List <TOCEntry>(); psHeader.MagicNumber = br.ReadUInt32(); psHeader.VersionNumber = br.ReadUInt32(); psHeader.CompressionMethod = br.ReadUInt32(); psHeader.TotalTOCSize = br.ReadUInt32(); psHeader.TOCEntrySize = br.ReadUInt32(); psHeader.numFiles = br.ReadUInt32(); psHeader.blockSize = br.ReadUInt32(); psHeader.archiveFlags = br.ReadUInt32(); for (uint i = 0; i < psHeader.numFiles; i++) { TOCEntry tmp = new TOCEntry(); tmp.MD5 = br.ReadBytes(16); tmp.blockListStart = br.ReadUInt32(); tmp.originalSize = FortyBitInt(br.ReadUInt64()); br.BaseStream.Position -= 3; tmp.startOffset = FortyBitInt(br.ReadUInt64()); br.BaseStream.Position -= 3; TOCs.Add(tmp); } // Extract the Manifest File br.BaseStream.Position = (long)TOCs[0].startOffset; // Manifest File byte[] CompressedStream = br.ReadBytes((int)(TOCs[1].startOffset - TOCs[0].startOffset)); byte[] DecompressedStream = zlib_net.Inflate(CompressedStream); File.WriteAllBytes(@"C:\Users\diwidog\Documents\Development Items\PC\pak23.manifest", DecompressedStream); br.Close(); }
private void ReadTOC(Stream S, TOC T) { ArrayList Entries = new ArrayList(); ArrayList SubTOCs = new ArrayList(); long BaseOffset = S.Position; TOCEntry TE = new TOCEntry(S, BaseOffset); bool ValidEntry = true; while (ValidEntry) { switch (TE.Type) { case EntryType.SubTOC: long Pos = S.Position; S.Seek(BaseOffset + TE.Offset_, SeekOrigin.Begin); TOC SubTOC = new TOC(); SubTOC.Name = TE.Name; this.ReadTOC(S, SubTOC); SubTOCs.Add(SubTOC); S.Seek(Pos, SeekOrigin.Begin); break; case EntryType.PNG: Entries.Add(TE); break; default: ValidEntry = false; break; } TE = new TOCEntry(S, BaseOffset); } T.SubTOCs = (TOC[])SubTOCs.ToArray(typeof(TOC)); T.Entries = (TOCEntry[])Entries.ToArray(typeof(TOCEntry)); }
private void BuildFS() { RootDirectory = new Directory(); RootDirectory.Name = "/"; int entryCount = _imgFile.Header.EntryCount; for (int i = 0; i < entryCount; i++) { TOCEntry entry = _imgFile.TOC[i]; Common.File.DataLoadDelegate load = () => LoadData(entry); Common.File.DataStoreDelegate store = data => StoreData(entry, data); Common.File.DataIsCustomDelegate isCustom = () => entry.CustomData != null; var file = new Common.File(load, store, isCustom) { CompressedSize = entry.Size, IsCompressed = false, Name = _imgFile.TOC.GetName(i), Size = entry.Size, IsResource = entry.IsResourceFile, ResourceType = entry.ResourceType, ParentDirectory = RootDirectory }; RootDirectory.AddObject(file); } }
private void BuildFS() { RootDirectory = new RPFLib.Common.Directory(); TOCEntry entry = _rpfFile.TOC[0]; BuildFSDirectory(entry as DirectoryEntry, RootDirectory); }
private string GetName(TOCEntry entry) { if (entry == _rpfFile.TOC[0]) { return("Root"); } return(_rpfFile.TOC.GetName(entry.NameOffset)); }
private void ReadSubTOC(Stream S, TreeNode Root) { long BaseOffset = S.Position; TOCEntry TE = new TOCEntry(S, BaseOffset); while (TE.Type == 0x4000) { TreeNode TN = new TreeNode(TE.Name); TN.Tag = TE; Root.Nodes.Add(TN); TE = new TOCEntry(S, BaseOffset); } }
public TOCListItem(TOCEntry entry) { Entry = entry; UseItemStyleForSubItems = false; typeItem = SubItems.Add(new ListViewSubItem()); sizeItem = SubItems.Add(new ListViewSubItem()); startItem = SubItems.Add(new ListViewSubItem()); endItem = SubItems.Add(new ListViewSubItem()); updateCaptions(); }
public static byte[] getDataByTOC(TOCEntry e) { byte[] data = new byte[e.Size]; FileStream fs = new FileStream(basePath + e.Container, FileMode.Open, FileAccess.Read); fs.Seek(e.RealOffset, 0); for (long i = 0; i < e.Size; i++) { data[i] = (byte)fs.ReadByte(); } fs.Close(); return(data); }
public static TOCEntry findTOCbyPath(string path) { TOCEntry result = new TOCEntry(); foreach (TOCEntry e in TOCList) { if (e.Name == path) { return(e); } } return(result); }
private byte[] LoadData(TOCEntry entry) { if (entry.CustomData == null) { byte[] data = _imgFile.ReadData(entry.OffsetBlock * 0x800, entry.Size); return(data); } else { return(entry.CustomData); } }
private void ReadTOC(Stream S, TreeNode Root) { long BaseOffset = S.Position; TOCEntry TE = new TOCEntry(S, BaseOffset); while (TE.Type == 0x8000) { long Pos = S.Position; S.Seek(BaseOffset + TE.Offset, SeekOrigin.Begin); this.ReadSubTOC(S, Root); S.Seek(Pos, SeekOrigin.Begin); TE = new TOCEntry(S, BaseOffset); } }
private void playSelectedTFM() { string samName = Path.ChangeExtension(selectedItem.Entry.Name, ".SAM"); if (!game.Assets.Entries.ContainsKey(samName)) { MessageBox.Show($"Sample file '{samName}' not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } TOCEntry samples = game.Assets.Entries[samName]; TFXTool.PlayerForm form = new TFXTool.PlayerForm(selectedItem.Entry.Name, selectedItem.Entry.Data, samples.Data); form.ShowDialog(this); }
private void BtnImport_Click(object sender, RoutedEventArgs e) { // PageNo,Songname, Composer, Date, Notes var clipText = System.Windows.Forms.Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text); if (!string.IsNullOrEmpty(clipText)) { var lstImportedToc = new List <TOCEntry>(); try { var lines = clipText.Split("\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var parts = line.Trim().Split("\t".ToArray()); var newEntry = new TOCEntry(); if (parts.Length > 0 && parts[0].Length > 0) { newEntry.PageNo = int.Parse(parts[0]); } if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { newEntry.SongName = parts[1].Trim(); } if (parts.Length > 2 && !string.IsNullOrEmpty(parts[2])) { newEntry.Composer = parts[2].Trim(); } if (parts.Length > 3 && !string.IsNullOrEmpty(parts[3])) { newEntry.Date = parts[3].Trim(); } if (parts.Length > 4 && !string.IsNullOrEmpty(parts[4])) { newEntry.Notes = parts[4].Trim(); } lstImportedToc.Add(newEntry); } } catch (Exception ex) { var loc = lstImportedToc.Count() == 0 ? "at first line" : $"after {lstImportedToc[lstImportedToc.Count() - 1]}"; System.Windows.Forms.MessageBox.Show($"Exception {loc} {ex.Message}"); } LstTOC = new ObservableCollection <TOCEntry>(lstImportedToc.OrderBy(p => p.PageNo)); } }
private static void LoadTOC() { Log.PrintLn("Loading manifest..."); string[] lines = File.ReadAllLines(basePath + "manifest"); TOCList = new List <TOCEntry>(); List <string> containerNames = new List <string>(); foreach (string line in lines) { TOCEntry e = new TOCEntry(); string[] p1 = line.Split('\t'); e.Container = p1[0]; string[] p2 = p1[1].Trim().Split(' '); e.Name = p2[0]; e.Offset = Convert.ToInt64(p2[1]); e.Size = Convert.ToInt64(p2[2]); TOCList.Add(e); containerNames.Add(e.Container); } Log.PrintLn("Looking up Containers..."); containerNames = containerNames.Distinct().ToList(); containerNames.Sort(); container = new List <Container>(); long offset = 0; foreach (string c in containerNames) { FileStream fs = new FileStream(basePath + c, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.End); Container con = new Container(); con.name = c; con.offset = offset; offset += fs.Position; container.Add(con); fs.Close(); } Log.PrintLn("Updating TOC List..."); for (int i = 0; i < TOCList.Count; i++) { TOCEntry e = TOCList[i]; e.RealOffset = e.Offset - getContainerOffset(e.Container); TOCList[i] = e; } }
private static void LoadTOC() { Log.PrintLn("Loading manifest..."); string[] lines = File.ReadAllLines(basePath + "manifest"); TOCList = new List<TOCEntry>(); List<string> containerNames = new List<string>(); foreach (string line in lines) { TOCEntry e = new TOCEntry(); string[] p1 = line.Split('\t'); e.Container = p1[0]; string[] p2 = p1[1].Trim().Split(' '); e.Name = p2[0]; e.Offset = Convert.ToInt64(p2[1]); e.Size = Convert.ToInt64(p2[2]); TOCList.Add(e); containerNames.Add(e.Container); } Log.PrintLn("Looking up Containers..."); containerNames = containerNames.Distinct().ToList(); containerNames.Sort(); container = new List<Container>(); long offset = 0; foreach (string c in containerNames) { FileStream fs = new FileStream(basePath + c, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.End); Container con = new Container(); con.name = c; con.offset = offset; offset += fs.Position; container.Add(con); fs.Close(); } Log.PrintLn("Updating TOC List..."); for (int i = 0; i < TOCList.Count; i++) { TOCEntry e = TOCList[i]; e.RealOffset = e.Offset - getContainerOffset(e.Container); TOCList[i] = e; } }
public PackedFile CompressFile(String fileName, byte[] binaryFile) { var tmpHeader = new Header(true); var tmpEntry = new TOCEntry(); tmpEntry.FileName = fileName; tmpEntry.MD5 = new byte[16]; tmpEntry.OriginalSize = (ulong)binaryFile.LongLength; tmpEntry.StartOffset = 0; tmpEntry.BlockListStart = 0; try { byte[] compressedFile = zlib_net.Deflate(binaryFile, tmpHeader.BlockSize); return(new PackedFile(tmpEntry, compressedFile)); } catch { } return(new PackedFile()); }
public PackedFile CompressFile(String fileName, byte[] binaryFile) { var tmpHeader = new Header(true); var tmpEntry = new TOCEntry(); tmpEntry.FileName = fileName.Replace('\\', '/'); tmpEntry.MD5 = new byte[16]; tmpEntry.OriginalSize = (ulong)binaryFile.LongLength; tmpEntry.StartOffset = 0; tmpEntry.BlockListStart = (uint)(Math.Ceiling(tmpEntry.OriginalSize / (double)tmpHeader.BlockSize)); try { byte[] compressedFile = ZlibUtils.Deflate(binaryFile, tmpHeader.BlockSize); return(new PackedFile(tmpEntry, compressedFile)); } catch { } return(new PackedFile()); }
private void BuildFSDirectory(DirectoryEntry dirEntry, Directory fsDirectory) { fsDirectory.Name = GetName(dirEntry); for (int i = 0; i < dirEntry.ContentEntryCount; i++) { TOCEntry entry = _rpfFile.TOC[dirEntry.ContentEntryIndex + i]; if (entry.IsDirectory) { var dir = new Directory(); BuildFSDirectory(entry as DirectoryEntry, dir); dir.ParentDirectory = fsDirectory; fsDirectory.AddObject(dir); } else { var fileEntry = entry as FileEntry; Common.File.DataLoadDelegate load = () => LoadData(fileEntry); Common.File.DataStoreDelegate store = data => StoreData(fileEntry, data); Common.File.DataIsCustomDelegate isCustom = () => fileEntry.CustomData != null; var file = new Common.File(load, store, isCustom); file.CompressedSize = fileEntry.SizeInArchive; file.IsCompressed = fileEntry.IsCompressed; file.Name = GetName(fileEntry); file.Size = fileEntry.Size; file.IsResource = fileEntry.IsResourceFile; file.ResourceType = fileEntry.ResourceType; file.ParentDirectory = fsDirectory; fsDirectory.AddObject(file); } } }
private GameFile GetFile(TOCEntry entry) { GameFile unpackFile; BinaryReaderBE br = new BinaryReaderBE(this.file.BaseStream); // Check compression this.file.BaseStream.Position = this.file.Position + entry.Offset; if (Zlib.IsValid(br.ReadUInt16())) { unpackFile = new GameFile(new MemoryStream((int)entry.Size)); Zlib zlib = new Zlib(this.file.BaseStream, this.file.Position + entry.Offset, -1); zlib.Decode(unpackFile.BaseStream, entry.Size); } else { unpackFile = new GameFile(this.file.BaseStream); unpackFile.Position = this.file.Position + entry.Offset; unpackFile.Parent = this.file; this.file.AddChild(unpackFile); } unpackFile.Size = entry.Size; return(unpackFile); }
public override GameFile[] Unpack() { this.file.BaseStream.Position = this.file.Position; BinaryReaderBE br = new BinaryReaderBE(this.file.BaseStream); // Read header if (br.ReadUInt32() != MagicStamp || br.ReadUInt32() != Version || br.ReadUInt32() != Compression) { throw new InvalidDataException(); } uint offsetData = br.ReadUInt32(); uint tocEntrySize = br.ReadUInt32(); uint numFiles = br.ReadUInt32(); uint unknown1 = br.ReadUInt32(); uint unknown2 = br.ReadUInt32(); // Read the TOC int fileListIndex = -1; TOCEntry[] toc = new TOCEntry[numFiles]; for (int i = 0; i < numFiles; i++) { br.BaseStream.Position = this.file.Position + TocOffset + i * tocEntrySize; // Read entry toc[i].Md5Name = BitConverter.ToString(br.ReadBytes(0x10)); toc[i].Block = br.ReadUInt32(); toc[i].Size = br.ReadInt64() >> 24; // Only 5 bytes br.BaseStream.Position -= 3; toc[i].Offset = br.ReadInt64() >> 24; // Only 5 bytes if (toc[i].Md5Name == Md5Null) { fileListIndex = i; } } // Read the file name table if (fileListIndex == -1) { throw new InvalidDataException("Error, file list NOT FOUND!"); } GameFile listFile = this.GetFile(toc[fileListIndex]); BinaryReader brList = new BinaryReader(listFile.BaseStream); brList.BaseStream.Position = listFile.Position; byte[] listData = brList.ReadBytes((int)listFile.Size); string[] list = DefaultEncoding.GetString(listData).Split('\n'); if (listFile.Parent == null) { listFile.BaseStream.Close(); listFile.BaseStream.Dispose(); } // For each entry search it relative path List <GameFile> files = new List <GameFile>(); for (int i = 0; i < list.Length; i++) { // Matchs the md5 codes string md5 = ComputeMd5(list[i]); TOCEntry entry = new TOCEntry(); for (int j = 0; j < toc.Length; j++) { if (md5 == toc[j].Md5Name) { entry = toc[j]; break; } } if (entry.Md5Name == null) { throw new FileNotFoundException("Relative path: " + list[i] + " NOT FOUND!"); } GameFile unpackFile = this.GetFile(entry); unpackFile.FileName = list[i].Substring(list[i].LastIndexOf('/') + 1); string relativePath = list[i].Substring(1, list[i].LastIndexOf('/')); //relativePath = relativePath.Replace('/', '\\'); unpackFile.FilePath = Path.Combine(this.file.FilePath, relativePath); files.Add(unpackFile); } br = null; return(files.ToArray()); }
private void StoreData(TOCEntry entry, byte[] data) { entry.SetCustomData(data); }
public static TOCEntry findTOCbyPath(string path) { TOCEntry result = new TOCEntry(); foreach (TOCEntry e in TOCList) if (e.Name == path) return e; return result; }
private void ShowImage() { if (this.tvDataFiles.SelectedNode == null) { return; } TOCEntry TE = this.tvDataFiles.SelectedNode.Tag as TOCEntry; if (TE == null) { return; } TE.Source.Seek(TE.Offset, SeekOrigin.Begin); try { Image I = null; { // Create buffer and make the image from that - Image.FromStream(this.CurrentFile) does NOT work BinaryReader BR = new BinaryReader(TE.Source); byte[] ImageData = BR.ReadBytes(TE.Size); MemoryStream MS = new MemoryStream(ImageData, false); I = Image.FromStream(MS); MS.Close(); } if (this.mnuTiledImage.Checked) { this.picViewer.BackgroundImage = I; } else { this.picViewer.Image = I; } this.picViewer.SizeMode = (this.mnuStretchImage.Checked ? PictureBoxSizeMode.StretchImage : PictureBoxSizeMode.CenterImage); int bitdepth = 0; switch (I.PixelFormat) { case PixelFormat.Format1bppIndexed: bitdepth = 1; break; case PixelFormat.Format4bppIndexed: bitdepth = 4; break; case PixelFormat.Format8bppIndexed: bitdepth = 8; break; case PixelFormat.Format16bppGrayScale: case PixelFormat.Format16bppArgb1555: case PixelFormat.Format16bppRgb555: case PixelFormat.Format16bppRgb565: bitdepth = 16; break; case PixelFormat.Format24bppRgb: bitdepth = 24; break; case PixelFormat.Format32bppArgb: case PixelFormat.Format32bppPArgb: case PixelFormat.Format32bppRgb: bitdepth = 32; break; case PixelFormat.Format48bppRgb: bitdepth = 48; break; case PixelFormat.Format64bppArgb: case PixelFormat.Format64bppPArgb: bitdepth = 64; break; } //ImageConverter IC = new ImageConverter(); this.sbrStatus.Text = String.Format("PNG Image - {0}x{1} {2}bpp", I.Width, I.Height, bitdepth); } catch {} }
private void BuildFSDirectory(DirectoryEntry dirEntry, RPFLib.Common.Directory fsDirectory) { try { fsDirectory.Name = GetName(dirEntry); for (int i = 0; i < dirEntry.ContentEntryCount; i++) { TOCEntry entry = _rpfFile.TOC[dirEntry.ContentEntryIndex + i]; if (entry.IsDirectory) { var subdirEntry = entry as DirectoryEntry; var dir = new RPFLib.Common.Directory(); dir._Contentcount = nCount => subdirEntry.setContentcount(nCount); dir._ContentIndex = ContentIndex => subdirEntry.setContentIndex(ContentIndex); dir._Index = NewContentIndex => subdirEntry.setNewContentIndex(NewContentIndex); dir.nameHash = (uint)subdirEntry.NameOffset; dir.ParentDirectory = fsDirectory; dir.Attributes = "Folder"; BuildFSDirectory(entry as DirectoryEntry, dir); fsDirectory.AddObject(dir); } else { var fileEntry = entry as FileEntry; var file = new Common.File(); file._dataLoad = getCustom => LoadData(fileEntry, getCustom); file._dataStore = data => StoreData(fileEntry, data); file._dataCustom = () => fileEntry.CustomData != null; file.d1 = () => fileEntry.getSize(); file._Index = nIndex => fileEntry.setIndex(nIndex); file._delete = () => fileEntry.Delete(); file.CompressedSize = fileEntry.SizeInArchive; file.IsCompressed = fileEntry.IsCompressed; file.nameHash = (uint)fileEntry.NameOffset; file.Name = GetName(fileEntry); file.IsResource = fileEntry.IsResourceFile; file.resourcetype = (int)fileEntry.ResourceType;//Convert.ToString((ResourceType)fileEntry.ResourceType); file.ParentDirectory = fsDirectory; StringBuilder attributes = new StringBuilder(); if (file.IsResource) { attributes.Append(string.Format("Resource [Version {0}", fileEntry.ResourceType)); if (file.IsCompressed) { attributes.Append(", Compressed"); } attributes.Append("]"); } else if (file.IsCompressed) { attributes.Append("Compressed"); } else { attributes.Append("None"); } file.Attributes = attributes.ToString(); fsDirectory.AddObject(file); } } } catch (System.Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); } }
public static byte[] getDataByTOC(TOCEntry e) { byte[] data = new byte[e.Size]; FileStream fs = new FileStream(basePath + e.Container, FileMode.Open, FileAccess.Read); fs.Seek(e.RealOffset, 0); for (long i = 0; i < e.Size; i++) data[i] = (byte)fs.ReadByte(); fs.Close(); return data; }