Exemplo n.º 1
0
        protected DatFile BuildGenericDatFile(DatFileAttribute attribute)
        {
            DatHeader header = new DatHeader(Convert.ToUInt32(attribute.Translations.Count));
            List <IndexTableRecord> indexTableRecords = new List <IndexTableRecord>();
            List <ValueTableRecord> valueTableRecords = new List <ValueTableRecord>();
            List <KeyTableRecord>   keyTableRecords   = new List <KeyTableRecord>();

            foreach (Translation translation in attribute.Translations)
            {
                ValueTableRecord valueTableRecord = new ValueTableRecord(translation.Value);
                valueTableRecords.Add(valueTableRecord);
                KeyTableRecord keyTableRecord = new KeyTableRecord(translation.Key);
                keyTableRecords.Add(keyTableRecord);
                IndexTableRecord indexTableRecord = new IndexTableRecord(
                    translation.Crc32,
                    Convert.ToUInt32(keyTableRecord.Key.Length),
                    Convert.ToUInt32(valueTableRecord.Value.Length));
                indexTableRecords.Add(indexTableRecord);
            }

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

            return(new DatFile(header, indexTable, valueTable, keyTable));
        }
Exemplo n.º 2
0
        private KeyTable BuildKeyTable(byte[] bytes, IndexTable indexTable, uint keyCount)
        {
            List <KeyTableRecord> keyTableRecords = new List <KeyTableRecord>();

            for (int i = 0; i < keyCount; i++)
            {
                long           keyLength      = indexTable.IndexTableRecords[i].KeyLength;
                KeyTableRecord keyTableRecord = new KeyTableRecord(bytes, _currentOffset, keyLength);
                _currentOffset += Convert.ToInt32(keyTableRecord.Size());
                keyTableRecords.Add(keyTableRecord);
            }

            return(new KeyTable(keyTableRecords));
        }
        private KeyTable BuildKeyTableFromBytesInternal([NotNull] byte[] bytes, [NotNull] IndexTable indexTable,
                                                        uint keyCount)
        {
            List <KeyTableRecord> keyTableRecords = new List <KeyTableRecord>();

            for (int i = 0; i < keyCount; i++)
            {
                long keyLength = indexTable.IndexTableRecords[i]?.KeyLength ??
                                 throw new IndexTableInvalidException(
                                           $"Building the DAT file failed at offset {m_currentOffset} due to an invalid IndexTableRecord at position {i}");
                KeyTableRecord keyTableRecord = new KeyTableRecord(bytes, m_currentOffset, keyLength);
                m_currentOffset += keyTableRecord.Size;
                keyTableRecords.Add(keyTableRecord);
            }

            return(new KeyTable(keyTableRecords));
        }
        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));
        }