Ejemplo n.º 1
0
        public override int GetHashCode(MpqFile mpqFile)
        {
            if (mpqFile is null)
            {
                throw new ArgumentNullException(nameof(mpqFile));
            }

            return(_ignoreLocale ? HashCode.Combine(mpqFile.Name) : mpqFile.GetHashCode());
        }
Ejemplo n.º 2
0
        // TODO: set hashCollisions values (currently they're always set to 0)
        public IEnumerable <MpqFile> GetMpqFiles()
        {
            // var pairs = new Dictionary<MpqEntry, (uint index, MpqFile file)>();
            var files        = new MpqFile[_blockTable.Size]; // array assumes there's no more than one mpqhash for every mpqentry
            var addedEntries = new HashSet <MpqEntry>();

            for (var hashIndex = 0; hashIndex < _hashTable.Size; hashIndex++)
            {
                var mpqHash = _hashTable[hashIndex];
                if (!mpqHash.IsEmpty)
                {
                    var mpqEntry = mpqHash.IsValidBlockIndex ? _blockTable[mpqHash.BlockIndex] : null;
                    if (mpqEntry != null)
                    {
                        // var stream = mpqHash.IsDeleted ? null : OpenFile(mpqEntry);
                        var stream  = OpenFile(mpqEntry);
                        var mpqFile = mpqEntry.Filename is null
                            ? MpqFile.New(stream, mpqHash, (uint)hashIndex, 0, mpqEntry.BaseEncryptionSeed)
                            : MpqFile.New(stream, mpqEntry.Filename, mpqHash.Locale);

                        mpqFile.TargetFlags = mpqEntry.Flags & ~MpqFileFlags.Garbage;

                        files[mpqHash.BlockIndex] = mpqFile;
                        addedEntries.Add(mpqEntry); // TODO: use returned bool to check 'duplicate' mpqhashes (which have same blockindex)
                    }
                }
            }

            for (var i = 0; i < Count; i++)
            {
                var mpqEntry = this[i];
                if (!addedEntries.Contains(mpqEntry))
                {
                    var mpqFile = MpqFile.New(OpenFile(mpqEntry));
                    mpqFile.TargetFlags = 0;
                    files[i]            = mpqFile;
                }
            }

            return(files);
        }
Ejemplo n.º 3
0
        // TODO: set hashCollisions values (currently they're always set to 0)
        public IEnumerable <MpqFile> GetMpqFiles()
        {
            // var pairs = new Dictionary<MpqEntry, (uint index, MpqFile file)>();
            var files          = new MpqFile[_blockTable.Size]; // array assumes there's no more than one mpqhash for every mpqentry
            var addedEntries   = new HashSet <MpqEntry>();
            var deletedIndices = new Queue <int>();

            for (var hashIndex = 0; hashIndex < _hashTable.Size; hashIndex++)
            {
                var mpqHash = _hashTable[hashIndex];
                if (!mpqHash.IsEmpty)
                {
                    var mpqEntry = mpqHash.IsDeleted ? null : _blockTable[mpqHash.BlockIndex];

                    if (mpqEntry != null)
                    {
                        var stream  = mpqHash.IsDeleted ? null : OpenFile(mpqEntry);
                        var mpqFile = mpqEntry.Filename is null
                            ? MpqFile.New(stream, mpqHash, (uint)hashIndex, 0, mpqEntry.BaseEncryptionSeed)
                            : MpqFile.New(stream, mpqEntry.Filename);

                        mpqFile.TargetFlags = mpqEntry.Flags;
                        if (mpqEntry.Filename != null && Enum.IsDefined(typeof(MpqLocale), mpqHash.Locale))
                        {
                            mpqFile.Locale = mpqHash.Locale;
                        }

                        // pairs.Add(mpqEntry, (mpqHash.BlockIndex, mpqFile));
                        // files.Add(mpqHash.BlockIndex, mpqFile);
                        files[mpqHash.BlockIndex] = mpqFile;
                        addedEntries.Add(mpqEntry); // TODO: use returned bool to check 'duplicate' mpqhashes (which have same blockindex)
                    }
                    else
                    {
                        deletedIndices.Enqueue(hashIndex);
                    }
                }
            }

            var blockIndex = 0U;

            foreach (var mpqEntry in this)
            {
                //if (!pairs.ContainsKey(mpqEntry))
                if (!addedEntries.Contains(mpqEntry))
                {
                    var hashIndex = deletedIndices.Dequeue();
                    var mpqHash   = _hashTable[hashIndex];
                    var mpqFile   = mpqEntry.Filename is null
                        ? MpqFile.New(null, mpqHash, (uint)hashIndex, 0, mpqEntry.BaseEncryptionSeed)
                        : MpqFile.New(null, mpqEntry.Filename);

                    mpqFile.TargetFlags = 0;
                    if (mpqEntry.Filename != null && Enum.IsDefined(typeof(MpqLocale), mpqHash.Locale))
                    {
                        mpqFile.Locale = mpqHash.Locale;
                    }

                    // pairs.Add(mpqEntry, (blockIndex, mpqFile));
                    // files.Add(blockIndex, mpqFile);
                    files[blockIndex] = mpqFile;
                }

                blockIndex++;
            }

            // return pairs.OrderBy(pair => pair.Value.index).Select(pair => pair.Value.file);
            // return files.Values;
            return(files);
        }
Ejemplo n.º 4
0
 /// <exception cref="FileNotFoundException"></exception>
 public static Stream GetFile(MpqArchive archive, string path)
 {
     return(MpqFile.OpenRead(archive, path));
 }
Ejemplo n.º 5
0
 /// <exception cref="FileNotFoundException"></exception>
 public static Stream GetFile(string path)
 {
     return(MpqFile.OpenRead(path));
 }
Ejemplo n.º 6
0
 public static bool FileExists(MpqArchive archive, string path)
 {
     return(MpqFile.Exists(archive, path));
 }
Ejemplo n.º 7
0
 public static bool FileExists(string path)
 {
     return(MpqFile.Exists(path));
 }
Ejemplo n.º 8
0
 public abstract int GetHashCode(MpqFile mpqFile);
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MpqArchive"/> class.
        /// </summary>
        /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be <see langword="null"/>.</param>
        /// <param name="inputFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param>
        /// <param name="createOptions"></param>
        /// <param name="leaveOpen">If <see langword="false"/>, the given <paramref name="sourceStream"/> will be disposed when the <see cref="MpqArchive"/> is disposed.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is <see langword="null"/>.</exception>
        public MpqArchive(Stream?sourceStream, IEnumerable <MpqFile> inputFiles, MpqArchiveCreateOptions createOptions, bool leaveOpen = false)
        {
            if (inputFiles is null)
            {
                throw new ArgumentNullException(nameof(inputFiles));
            }

            if (createOptions is null)
            {
                throw new ArgumentNullException(nameof(createOptions));
            }

            _isStreamOwner = !leaveOpen;
            _baseStream    = AlignStream(sourceStream);

            _headerOffset         = _baseStream.Position;
            _blockSize            = BlockSizeModifier << createOptions.BlockSize;
            _archiveFollowsHeader = createOptions.WriteArchiveFirst;

            var signatureName  = Signature.FileName.GetStringHash();
            var listFileName   = ListFile.FileName.GetStringHash();
            var attributesName = Attributes.FileName.GetStringHash();

            var signatureCreateMode  = createOptions.SignatureCreateMode.GetValueOrDefault(MpqFileCreateMode.Prune);
            var listFileCreateMode   = createOptions.ListFileCreateMode.GetValueOrDefault(MpqFileCreateMode.Overwrite);
            var attributesCreateMode = createOptions.AttributesCreateMode.GetValueOrDefault(MpqFileCreateMode.Overwrite);
            var haveSignature        = false;
            var haveListFile         = false;
            var haveAttributes       = false;
            var mpqFiles             = new HashSet <MpqFile>(MpqFileComparer.Default);

            foreach (var mpqFile in inputFiles)
            {
                if (mpqFile is MpqOrphanedFile)
                {
                    continue;
                }

                if (mpqFile.Name == signatureName)
                {
                    if (signatureCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag))
                    {
                        continue;
                    }
                    else
                    {
                        haveSignature = true;
                    }
                }

                if (mpqFile.Name == listFileName)
                {
                    if (listFileCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag))
                    {
                        continue;
                    }
                    else
                    {
                        haveListFile = true;
                    }
                }

                if (mpqFile.Name == attributesName)
                {
                    if (attributesCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag))
                    {
                        continue;
                    }
                    else
                    {
                        haveAttributes = true;
                    }
                }

                if (!mpqFiles.Add(mpqFile))
                {
                    // todo: logging?
                }
            }

            var fileCount = (uint)mpqFiles.Count;

            var wantGenerateSignature = !haveSignature && signatureCreateMode.HasFlag(MpqFileCreateMode.AddFlag);
            var signature             = wantGenerateSignature ? new Signature() : null;

            if (wantGenerateSignature)
            {
                fileCount++;
            }

            var wantGenerateListFile = !haveListFile && listFileCreateMode.HasFlag(MpqFileCreateMode.AddFlag);
            var listFile             = wantGenerateListFile ? new ListFile() : null;

            if (wantGenerateListFile)
            {
                fileCount++;
            }

            var wantGenerateAttributes = !haveAttributes && attributesCreateMode.HasFlag(MpqFileCreateMode.AddFlag);
            var attributes             = wantGenerateAttributes ? new Attributes(createOptions) : null;

            if (wantGenerateAttributes)
            {
                fileCount++;
            }

            _hashTable  = new HashTable(Math.Max(createOptions.HashTableSize ?? fileCount * 8, fileCount));
            _blockTable = new BlockTable();

            using (var writer = new BinaryWriter(_baseStream, new UTF8Encoding(false, true), true))
            {
                // Skip the MPQ header, since its contents will be calculated afterwards.
                writer.Seek((int)MpqHeader.Size, SeekOrigin.Current);

                // Write Archive
                var fileIndex  = 0U;
                var fileOffset = _archiveFollowsHeader ? MpqHeader.Size : throw new NotImplementedException();

                // var gaps = new List<(long Start, long Length)>();
                var endOfStream = _baseStream.Position;

                void InsertMpqFile(MpqFile mpqFile, bool updateEndOfStream, bool allowMultiple = true)
                {
                    if (listFile is not null && mpqFile is MpqKnownFile knownFile)
                    {
                        listFile.FileNames.Add(knownFile.FileName);
                    }

                    mpqFile.AddToArchive(this, fileIndex, out var mpqEntry, out var mpqHash);
                    var hashTableEntries = _hashTable.Add(mpqHash, mpqFile.HashIndex, mpqFile.HashCollisions);

                    if (!allowMultiple && hashTableEntries > 1)
                    {
                        throw new Exception();
                    }

                    var crc32 = 0;

                    if (attributes is not null && attributes.Flags.HasFlag(AttributesFlags.Crc32) && allowMultiple)
                    {
                        mpqFile.MpqStream.Position = 0;
                        crc32 = new Ionic.Crc.CRC32().GetCrc32(mpqFile.MpqStream);
                    }

                    for (var i = 0; i < hashTableEntries; i++)
                    {
                        _blockTable.Add(mpqEntry);
                        if (attributes is not null)
                        {
                            if (attributes.Flags.HasFlag(AttributesFlags.Crc32))
                            {
                                attributes.Crc32s.Add(crc32);
                            }

                            if (attributes.Flags.HasFlag(AttributesFlags.DateTime))
                            {
                                attributes.DateTimes.Add(DateTime.Now);
                            }

                            if (attributes.Flags.HasFlag(AttributesFlags.Unk0x04))
                            {
                                attributes.Unk0x04s.Add(new byte[16]);
                            }
                        }
                    }

                    mpqFile.Dispose();

                    fileIndex += hashTableEntries;
                    if (updateEndOfStream)
                    {
                        endOfStream = _baseStream.Position;
                    }
                }

                // Find files that cannot be decrypted, and need to have a specific position in the archive, because that position is used to calculate the encryption seed.
                var mpqFixedPositionFiles = mpqFiles.Where(mpqFile => mpqFile.IsFilePositionFixed).OrderBy(mpqFile => mpqFile.MpqStream.FilePosition).ToArray();
                if (mpqFixedPositionFiles.Length > 0)
                {
                    if (mpqFixedPositionFiles.First() !.MpqStream.FilePosition < 0)
                    {
                        throw new NotSupportedException($"Cannot place files in front of the header.");
                    }

                    foreach (var mpqFixedPositionFile in mpqFixedPositionFiles)
                    {
                        var position = mpqFixedPositionFile.MpqStream.FilePosition;
                        if (position < endOfStream)
                        {
                            throw new ArgumentException($"Fixed position files overlap with each other and/or the header. Archive cannot be created.", nameof(inputFiles));
                        }

                        if (position > endOfStream)
                        {
                            var gapSize = position - endOfStream;
                            // gaps.Add((endOfStream, gapSize));
                            writer.Seek((int)gapSize, SeekOrigin.Current);
                        }

                        InsertMpqFile(mpqFixedPositionFile, true);
                    }
                }

                foreach (var mpqFile in mpqFiles.Where(mpqFile => !mpqFile.IsFilePositionFixed))
                {
                    // TODO: insert files into the gaps
                    // need to know compressed size of file first, and if file is also encrypted with blockoffsetadjustedkey, encryption needs to happen after gap selection
                    // therefore, can't use current AddToArchive method, which does both compression and encryption at same time

                    // var availableGaps = gaps.Where(gap => gap.Length >= )
                    var selectedPosition = endOfStream;
                    var selectedGap      = false;
                    _baseStream.Position = selectedPosition;

                    InsertMpqFile(mpqFile, !selectedGap);
                }

                var signaturePosition = endOfStream + 8;
                if (signature is not null)
                {
                    _baseStream.Position = endOfStream;

                    using var signatureStream = new MemoryStream();
                    using var signatureWriter = new BinaryWriter(signatureStream);
                    signatureWriter.Write(signature);
                    signatureWriter.Flush();

                    using var signatureMpqFile   = MpqFile.New(signatureStream, Signature.FileName);
                    signatureMpqFile.TargetFlags = MpqFileFlags.Exists;
                    InsertMpqFile(signatureMpqFile, true);
                }

                if (listFile is not null)
                {
                    _baseStream.Position = endOfStream;

                    using var listFileStream = new MemoryStream();
                    using var listFileWriter = new StreamWriter(listFileStream);
                    listFileWriter.WriteListFile(listFile);
                    listFileWriter.Flush();

                    using var listFileMpqFile   = MpqFile.New(listFileStream, ListFile.FileName);
                    listFileMpqFile.TargetFlags = MpqFileFlags.Exists | MpqFileFlags.CompressedMulti | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey;
                    InsertMpqFile(listFileMpqFile, true);
                }

                if (attributes is not null)
                {
                    _baseStream.Position = endOfStream;

                    if (attributes.Flags.HasFlag(AttributesFlags.Crc32))
                    {
                        attributes.Crc32s.Add(0);
                    }

                    if (attributes.Flags.HasFlag(AttributesFlags.DateTime))
                    {
                        attributes.DateTimes.Add(DateTime.Now);
                    }

                    if (attributes.Flags.HasFlag(AttributesFlags.Unk0x04))
                    {
                        attributes.Unk0x04s.Add(new byte[16]);
                    }

                    using var attributesStream = new MemoryStream();
                    using var attributesWriter = new BinaryWriter(attributesStream);
                    attributesWriter.Write(attributes);
                    attributesWriter.Flush();

                    using var attributesMpqFile   = MpqFile.New(attributesStream, Attributes.FileName);
                    attributesMpqFile.TargetFlags = MpqFileFlags.Exists | MpqFileFlags.CompressedMulti | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey;
                    InsertMpqFile(attributesMpqFile, true, false);
                }

                _baseStream.Position = endOfStream;
                _hashTable.WriteTo(writer);
                _blockTable.WriteTo(writer);

                /*if (!_archiveFollowsHeader)
                 * {
                 *  foreach (var mpqFile in mpqFiles)
                 *  {
                 *      mpqFile.WriteTo(writer, true);
                 *  }
                 * }*/

                writer.Seek((int)_headerOffset, SeekOrigin.Begin);

                _mpqHeader = new MpqHeader((uint)_headerOffset, (uint)(endOfStream - fileOffset), _hashTable.Size, _blockTable.Size, createOptions.BlockSize, _archiveFollowsHeader);
                _mpqHeader.WriteTo(writer);

                if (wantGenerateSignature)
                {
                    var archiveBytes = new byte[_mpqHeader.ArchiveSize];
                    _baseStream.Position = _headerOffset;
                    _baseStream.Read(archiveBytes);

                    using var rsa = RSA.Create();

                    rsa.ImportFromPem(createOptions.SignaturePrivateKey);
                    var signatureBytes = rsa.SignData(archiveBytes, HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);

                    _baseStream.Position = signaturePosition;
                    _baseStream.Write(signatureBytes.Reverse().ToArray());
                }
            }
        }