예제 #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 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));
        }
예제 #3
0
 public int CompareTo(KeyTableRecord other)
 {
     if (other == null)
     {
         return(0);
     }
     if (ChecksumUtility.GetChecksum(Key) > ChecksumUtility.GetChecksum(other.Key))
     {
         return(1);
     }
     if (ChecksumUtility.GetChecksum(Key) < ChecksumUtility.GetChecksum(other.Key))
     {
         return(-1);
     }
     return(0);
 }
        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));
        }
        protected DatFile FromHolderInternal([NotNull] List <Tuple <string, string> > content)
        {
            DatHeader datHeader = new DatHeader(Convert.ToUInt32(content.Count));
            List <IndexTableRecord> indexTableRecords = new List <IndexTableRecord>();
            List <ValueTableRecord> valueTableRecords = new List <ValueTableRecord>();
            List <KeyTableRecord>   keyTableRecords   = new List <KeyTableRecord>();

            foreach (Tuple <string, string> keyValuePair in content)
            {
                if (null == keyValuePair)
                {
                    continue;
                }

                string key   = keyValuePair.Item1?.Replace("\0", string.Empty) ?? string.Empty;
                string value = keyValuePair.Item2;

                if (StringUtility.IsNullEmptyOrWhiteSpace(key))
                {
                    throw new DatFileContentInvalidException(
                              $"A key may never be null. Provided key-value-pair {keyValuePair.Item1}:{keyValuePair.Item2}");
                }

                if (null == value)
                {
                    value = "";
                }

                ValueTableRecord valueTableRecord = new ValueTableRecord(value);
                KeyTableRecord   keyTableRecord   = new KeyTableRecord(key);
                IndexTableRecord indexTableRecord = new IndexTableRecord(ChecksumUtility.GetChecksum(key),
                                                                         Convert.ToUInt32(key.Length), Convert.ToUInt32(value.Length));

                valueTableRecords.Add(valueTableRecord);
                keyTableRecords.Add(keyTableRecord);
                indexTableRecords.Add(indexTableRecord);
            }

            IndexTable indexTable = new IndexTable(indexTableRecords);
            ValueTable valueTable = new ValueTable(valueTableRecords);
            KeyTable   keyTable   = new KeyTable(keyTableRecords);

            return(new DatFile(datHeader, indexTable, valueTable, keyTable));
        }
            public int Compare(object x, object y)
            {
                if (x == null || y == null)
                {
                    return(0);
                }

                if (!(x is MegFileNameTableRecord a) || !(y is MegFileNameTableRecord b))
                {
                    return(0);
                }

                if (ChecksumUtility.GetChecksum(a.FileName) < ChecksumUtility.GetChecksum(b.FileName))
                {
                    return(-1);
                }

                return(1);
            }
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(0);
            }

            MegFileNameTableRecord b = obj as MegFileNameTableRecord;

            if (b != null && ChecksumUtility.GetChecksum(FileName) > ChecksumUtility.GetChecksum(b.FileName))
            {
                return(1);
            }

            if (b != null && ChecksumUtility.GetChecksum(FileName) < ChecksumUtility.GetChecksum(b.FileName))
            {
                return(-1);
            }

            return(0);
        }
 public override int GetHashCode()
 {
     return((int)ChecksumUtility.GetChecksum(Key));
 }