private HdrHeader ReadHeader(IBuffer buffer)
        {
            HdrHeader header = new HdrHeader();
            string    first  = buffer.ReadCString();

            if (first == Hdr)
            {
                header.Format  = first;
                header.Created = null;
            }
            else if (DateTime.TryParseExact(first, DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None,
                                            out var created))
            {
                header.Created = created;
                header.Format  = buffer.ReadCString();
            }
            else
            {
                throw new Exception("Can not read header, this is not a hdr file or the file is broken.");
            }
            header.Unknown0      = buffer.ReadInt32();
            header.ContentOffset = buffer.ReadInt32();
            header.FolderCount   = buffer.ReadInt32();
            header.IndexOffset   = buffer.ReadInt32();
            return(header);
        }
        public void Pack(string source, string destination)
        {
            if (!Path.HasExtension(destination))
            {
                throw new Exception(String.Format("Destination '{0}' has no .ext, please use '.tro' or '.dat'",
                                                  destination));
            }
            string    ext = Path.GetExtension(destination).ToLower();
            HdrHeader header;

            if (ext == ".tro")
            {
                header = HdrHeader.ForTro();
            }
            else if (ext == ".dat")
            {
                header = HdrHeader.ForDat();
            }
            else
            {
                throw new Exception(
                          String.Format("Destination has invalid extension of '{0}', please use '.tro' or '.dat'", ext));
            }
            List <HdrFile> hdrFiles = ReadDirectory(source);
            HdrArchive     archive  = new HdrArchive(hdrFiles, header);

            Write(archive, destination);
        }
        public HdrArchive Read(string sourcePath)
        {
            byte[]         hdrFile          = ReadFile(sourcePath);
            IBuffer        buffer           = BufferProvider.Provide(hdrFile);
            HdrHeader      header           = ReadHeader(buffer);
            List <HdrFile> files            = new List <HdrFile>();
            int            folderIndexStart = header.IndexOffset;
            int            totalFiles       = 0;
            int            currentFile      = 0;

            for (int i = 0; i < header.FolderCount; i++)
            {
                buffer.Position = folderIndexStart + i * IndexBlockSize;
                HdrIndex folderIndex = ReadIndex(buffer);
                buffer.Position = folderIndex.Offset;
                for (int j = 0; j < folderIndex.Length; j++)
                {
                    HdrIndex fileIndex = ReadIndex(buffer);
                    int      offset    = fileIndex.Offset;
                    int      lenght    = fileIndex.Length;
                    string   ext;
                    if (Path.HasExtension(fileIndex.Name))
                    {
                        ext = Path.GetExtension(fileIndex.Name);
                        switch (ext)
                        {
                        case ".pts":
                            // PTS-Files are PNG files with the first 4byte beeing lengt of the data.
                            // We already know the length from the index.
                            // offset += 4;
                            // lenght -= 4;
                            break;
                        }
                    }
                    else
                    {
                        ext = "";
                    }
                    HdrFile file = new HdrFile();
                    file.FileExtension    = ext;
                    file.FileName         = fileIndex.Name;
                    file.HdrDirectoryPath = folderIndex.Name;
                    file.HdrFullPath      = folderIndex.Name + fileIndex.Name;
                    file.Data             = buffer.GetBytes(offset, lenght);
                    file.Offset           = offset;
                    file.Length           = lenght;
                    file.Extension        = ext;
                    files.Add(file);
                    currentFile++;
                }
                totalFiles += folderIndex.Length;
                OnProgressChanged(totalFiles, currentFile);
            }
            return(new HdrArchive(files, header));
        }
Exemple #4
0
        public static HdrHeader ForTro()
        {
            HdrHeader header = new HdrHeader();

            header.Format      = HdrFormat.Hdr;
            header.Created     = null;
            header.Unknown0    = 1;
            header.IndexOffset = 20;
            header.ArchiveType = HdrArchiveType.Tro;
            return(header);
        }
Exemple #5
0
        public static HdrHeader ForDat()
        {
            HdrHeader header = new HdrHeader();

            header.Format      = HdrFormat.Hdr;
            header.Created     = DateTime.Now;
            header.Unknown0    = 1;
            header.IndexOffset = 40;
            header.ArchiveType = HdrArchiveType.Dat;
            return(header);
        }
 private void WriteHeader(IBuffer buffer, HdrHeader header)
 {
     if (header.Created != null)
     {
         buffer.WriteCString(string.Format("{0:" + DateFormat + "}", header.Created));
     }
     buffer.WriteCString(header.Format);
     buffer.WriteInt32(header.Unknown0);
     buffer.WriteInt32(header.ContentOffset);
     buffer.WriteInt32(header.FolderCount);
     buffer.WriteInt32(header.IndexOffset);
 }
Exemple #7
0
 public HdrArchive()
 {
     Files  = new List <HdrFile>();
     Header = new HdrHeader();
 }
Exemple #8
0
 public HdrArchive(List <HdrFile> files, HdrHeader header)
 {
     Files  = files;
     Header = header;
 }
        public void Write(HdrArchive archive, string destinationPath)
        {
            Dictionary <string, List <HdrFile> > folderDictionary = new Dictionary <string, List <HdrFile> >();
            List <string> orderedKeys = new List <string>();

            foreach (HdrFile file in archive.Files)
            {
                if (archive.Header.ArchiveType == HdrArchiveType.Tro)
                {
                    string[] folderNameParts = file.HdrDirectoryPath.Split('\\');
                    string   folderName      = "";
                    for (int i = 0; i < folderNameParts.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(folderNameParts[i]))
                        {
                            folderName += folderNameParts[i] + '\\';
                            if (!folderDictionary.ContainsKey(folderName))
                            {
                                folderDictionary.Add(folderName, new List <HdrFile>());
                                orderedKeys.Add(folderName);
                            }
                        }
                    }
                }
                if (folderDictionary.ContainsKey(file.HdrDirectoryPath))
                {
                    folderDictionary[file.HdrDirectoryPath].Add(file);
                }
                else
                {
                    folderDictionary.Add(file.HdrDirectoryPath, new List <HdrFile>()
                    {
                        file
                    });
                    orderedKeys.Add(file.HdrDirectoryPath);
                }
            }
            orderedKeys.Sort((s1, s2) => string.Compare(s1, s2, StringComparison.InvariantCultureIgnoreCase));
            IBuffer buffer               = BufferProvider.Provide();
            int     totalFiles           = archive.Files.Count;
            int     currentFile          = 0;
            int     folderIndexStart     = archive.Header.IndexOffset;
            int     fileIndexStart       = folderIndexStart + folderDictionary.Count * IndexBlockSize;
            int     contentStart         = fileIndexStart + IndexBlockSize * archive.Files.Count;
            int     currentFolderIndex   = 0;
            int     currentFileIndex     = 0;
            int     currentContentLength = 0;

            foreach (string key in orderedKeys)
            {
                HdrIndex folderIndex = new HdrIndex();
                folderIndex.Name     = key;
                folderIndex.Length   = folderDictionary[key].Count;
                folderIndex.Position = folderIndexStart + currentFolderIndex;
                if (folderIndex.Length > 0)
                {
                    folderIndex.Offset = fileIndexStart + currentFileIndex;
                }
                else
                {
                    folderIndex.Offset = 0;
                }
                WriteIndex(buffer, folderIndex);
                foreach (HdrFile file in folderDictionary[key])
                {
                    HdrIndex fileIndex = new HdrIndex();
                    fileIndex.Name     = file.FileName;
                    fileIndex.Length   = file.Data.Length;
                    fileIndex.Position = fileIndexStart + currentFileIndex;
                    fileIndex.Offset   = contentStart + currentContentLength;
                    WriteIndex(buffer, fileIndex);
                    buffer.WriteBytes(file.Data, 0, fileIndex.Offset, fileIndex.Length);
                    currentFileIndex     += IndexBlockSize;
                    currentContentLength += file.Data.Length;
                    currentFile++;
                }
                currentFolderIndex += IndexBlockSize;
                OnProgressChanged(totalFiles, currentFile);
            }
            HdrHeader header = new HdrHeader();

            header.ContentOffset = contentStart;
            header.Created       = archive.Header.Created;
            header.FolderCount   = folderDictionary.Count;
            header.Format        = Hdr;
            header.Unknown0      = archive.Header.Unknown0;
            header.IndexOffset   = folderIndexStart;
            buffer.Position      = 0;
            WriteHeader(buffer, header);
            WriteFile(buffer.GetAllBytes(), destinationPath);
        }