Esempio n. 1
0
        private MegFileContentTable BuildFileContentTables(MegFileAttribute megFileAttribute,
                                                           MegFileNameTable megFileNameTable)
        {
            List <string> absoluteFilePaths = megFileAttribute.ContentFiles.ToList();
            List <MegFileContentTableRecord> megFileContentList = new List <MegFileContentTableRecord>();
            uint currentOffset = new MegHeader(0, 0).Size();

            currentOffset += megFileNameTable.Size();

            for (int i = 0; i < megFileNameTable.MegFileNameTableRecords.Count; i++)
            {
                uint crc32              = ChecksumUtility.GetChecksum(megFileNameTable.MegFileNameTableRecords[i].FileName);
                uint fileSizeInBytes    = Convert.ToUInt32(new FileInfo(absoluteFilePaths[i]).Length);
                uint fileNameTableIndex = Convert.ToUInt32(i);
                MegFileContentTableRecord megFileContentTableRecord = new MegFileContentTableRecord(crc32, 0, fileSizeInBytes, 0, fileNameTableIndex);
                megFileContentList.Add(megFileContentTableRecord);
                currentOffset += megFileContentTableRecord.Size();
            }

            megFileContentList.Sort();

            for (int i = 0; i < megFileContentList.Count; i++)
            {
                megFileContentList[i].FileTableRecordIndex   = Convert.ToUInt32(i);
                megFileContentList[i].FileStartOffsetInBytes = currentOffset;
                currentOffset += megFileContentList[i].FileSizeInBytes;
            }

            return(new MegFileContentTable(megFileContentList));
        }
        public MegFile FromBytes(byte[] byteStream)
        {
            if (byteStream == null || byteStream.Length < 1)
            {
                throw new ArgumentNullException(nameof(byteStream), "The provided file is empty.");
            }

            MegHeader           header              = BuildMegHeaderInternal(byteStream);
            MegFileNameTable    megFileNameTable    = BuildFileNameTableInternal(byteStream);
            MegFileContentTable megFileContentTable = BuildFileContentTableInternal(byteStream);

            return(new MegFile(header, megFileNameTable, megFileContentTable));
        }
Esempio n. 3
0
        public MegFile Build(byte[] megFileBytes)
        {
            if (megFileBytes == null || megFileBytes.Length < 1)
            {
                throw new ArgumentNullException(nameof(megFileBytes), "The provided file is empty.");
            }

            MegHeader           header              = BuildMegHeader(megFileBytes);
            MegFileNameTable    megFileNameTable    = BuildFileNameTable(megFileBytes);
            MegFileContentTable megFileContentTable = BuildFileContentTable(megFileBytes);

            return(new MegFile(header, megFileNameTable, megFileContentTable));
        }
        public MegFile FromHolder(MegFileHolder holder, out IList <string> filesToStream)
        {
            List <string> files = holder.Content.Select(megFileDataEntry => megFileDataEntry.RelativeFilePath).ToList();
            List <MegFileNameTableRecord> megFileNameTableRecords =
                files.Select(file => new MegFileNameTableRecord(file)).ToList();

            megFileNameTableRecords.Sort();
            filesToStream = new List <string>();
            foreach (MegFileNameTableRecord megFileNameTableRecord in megFileNameTableRecords)
            {
                foreach (MegFileDataEntry megFileDataEntry in CollectSortedMegFileDataEntries(holder, megFileNameTableRecord))
                {
                    filesToStream.Add(megFileDataEntry.AbsoluteFilePath);
                    break;
                }
            }

            List <MegFileContentTableRecord> megFileContentTableRecords = new List <MegFileContentTableRecord>();

            for (int i = 0; i < megFileNameTableRecords.Count; i++)
            {
                uint crc32 = ChecksumUtility.GetChecksum(megFileNameTableRecords[i].FileName);
                uint fileTableRecordIndex = Convert.ToUInt32(i);
                uint fileSizeInBytes      = Convert.ToUInt32(m_fileSystem.FileInfo.FromFileName(filesToStream[i]).Length);
                uint fileNameTableIndex   = Convert.ToUInt32(i);
                megFileContentTableRecords.Add(new MegFileContentTableRecord(crc32, fileTableRecordIndex,
                                                                             fileSizeInBytes, 0, fileNameTableIndex));
            }

            MegHeader header = new MegHeader(Convert.ToUInt32(megFileContentTableRecords.Count),
                                             Convert.ToUInt32(megFileContentTableRecords.Count));
            MegFileNameTable megFileNameTable = new MegFileNameTable(megFileNameTableRecords);
            uint             currentOffset    = Convert.ToUInt32(header.Size);

            currentOffset += Convert.ToUInt32(megFileNameTable.Size);
            MegFileContentTable t = new MegFileContentTable(megFileContentTableRecords);

            currentOffset += Convert.ToUInt32(t.Size);
            foreach (MegFileContentTableRecord megFileContentTableRecord in megFileContentTableRecords)
            {
                megFileContentTableRecord.FileStartOffsetInBytes = currentOffset;
                currentOffset += Convert.ToUInt32(megFileContentTableRecord.FileSizeInBytes);
            }

            MegFileContentTable megFileContentTable = new MegFileContentTable(megFileContentTableRecords);

            return(new MegFile(header, megFileNameTable, megFileContentTable));
        }
Esempio n. 5
0
        public MegFile Build(MegFileAttribute megFileAttribute)
        {
            MegHeader        megHeader        = BuildMegHeader(megFileAttribute);
            MegFileNameTable megFileNameTable = BuildFileNameTable(megFileAttribute);
            //uint currentFileSize = megHeader.Size() + megFileNameTable.Size();
            MegFileContentTable megFileContentTable =
                BuildFileContentTables(megFileAttribute, megFileNameTable);
            //BuildFileContentTable(megFileAttribute, megFileNameTable, currentFileSize);
            MegFile megFile =
                new MegFile(megHeader, megFileNameTable, megFileContentTable)
            {
                Files = megFileAttribute.ContentFiles.ToList()
            };

            return(megFile);
        }
        public MegFile FromHolder(MegFileHolder holder)
        {
            List <string> files = holder.Content.Select(megFileDataEntry => megFileDataEntry.RelativeFilePath).ToList();
            List <MegFileNameTableRecord> megFileNameTableRecords =
                files.Select(file => new MegFileNameTableRecord(file)).ToList();

            megFileNameTableRecords.Sort();
            // Workaround for Unix compatibility.
            // File names are always stored as uppercase and without delimiter (\0), but Unix's file system is case sensitive,
            // so we cache the proper file paths sorted by the "cleaned" version's CRC for quick access later.
            List <string> sortedFiles = (from megFileNameTableRecord in megFileNameTableRecords
                                         from file in files
                                         where megFileNameTableRecord.FileName.Equals(file, StringComparison.InvariantCultureIgnoreCase)
                                         select file).ToList();
            List <MegFileContentTableRecord> megFileContentTableRecords = new List <MegFileContentTableRecord>();

            for (int i = 0; i < megFileNameTableRecords.Count; i++)
            {
                uint crc32 = ChecksumUtility.GetChecksum(megFileNameTableRecords[i].FileName);
                uint fileTableRecordIndex = Convert.ToUInt32(i);
                uint fileSizeInBytes      = Convert.ToUInt32(m_fileSystem.FileInfo
                                                             .FromFileName(
                                                                 m_fileSystem.Path.GetFullPath(m_fileSystem.Path.Combine(holder.FilePath, sortedFiles[i])))
                                                             .Length);
                uint fileNameTableIndex = Convert.ToUInt32(i);
                megFileContentTableRecords.Add(new MegFileContentTableRecord(crc32, fileTableRecordIndex,
                                                                             fileSizeInBytes, 0, fileNameTableIndex));
            }

            MegHeader header = new MegHeader(Convert.ToUInt32(megFileContentTableRecords.Count),
                                             Convert.ToUInt32(megFileContentTableRecords.Count));
            MegFileNameTable megFileNameTable = new MegFileNameTable(megFileNameTableRecords);
            uint             currentOffset    = Convert.ToUInt32(header.Size);

            currentOffset += Convert.ToUInt32(megFileNameTable.Size);
            MegFileContentTable t = new MegFileContentTable(megFileContentTableRecords);

            currentOffset += Convert.ToUInt32(t.Size);
            foreach (MegFileContentTableRecord megFileContentTableRecord in megFileContentTableRecords)
            {
                megFileContentTableRecord.FileStartOffsetInBytes = currentOffset;
                currentOffset += Convert.ToUInt32(megFileContentTableRecord.FileSizeInBytes);
            }
            MegFileContentTable megFileContentTable = new MegFileContentTable(megFileContentTableRecords);

            return(new MegFile(header, megFileNameTable, megFileContentTable));
        }