public void RemoveFile(MpqArchive mpqArchive, int blockIndex) { foreach (var mpqHash in mpqArchive.HashTable._hashes) { if (mpqHash.BlockIndex == blockIndex) { RemoveFile(mpqHash.Name); } } }
public void RemoveFile(MpqArchive mpqArchive, MpqEntry mpqEntry) { var blockIndex = mpqArchive.BlockTable._entries.IndexOf(mpqEntry); if (blockIndex == -1) { throw new ArgumentException("The given mpq entry could not be found in the archive.", nameof(mpqEntry)); } RemoveFile(mpqArchive, blockIndex); }
/// <summary> /// Initializes a new instance of the <see cref="MpqStream"/> class. /// </summary> /// <param name="archive"></param> /// <param name="entry"></param> internal MpqStream(MpqArchive archive, MpqEntry entry) { _entry = entry; _stream = archive.BaseStream; _blockSize = archive.BlockSize; if (_entry.IsCompressed && !_entry.IsSingleUnit) { LoadBlockPositions(); } }
public MpqArchiveBuilder(MpqArchive originalMpqArchive) { if (originalMpqArchive is null) { throw new ArgumentNullException(nameof(originalMpqArchive)); } _originalHashTableSize = (ushort)originalMpqArchive.HashTable.Size; _originalFiles = new List <MpqFile>(originalMpqArchive.GetMpqFiles()); _modifiedFiles = new List <MpqFile>(); _removedFiles = new List <ulong>(); }
public static bool VerifyAttributes(this MpqArchive archive) { if (archive.TryAddFilename(Key)) { using var attributesStream = archive.OpenFile(Key); using var reader = new BinaryReader(attributesStream); var unk = reader.ReadUInt32(); if (unk != 100) { throw new InvalidDataException(); } var dataFlags = reader.ReadUInt32(); if (dataFlags <= 0 || dataFlags >= 4) { throw new InvalidDataException(); } // CRC32 if ((dataFlags & 0x1) != 0) { foreach (var mpqEntry in archive) { using var mpqEntryStream = archive.OpenFile(mpqEntry); var expected = mpqEntry.Filename == Key ? 0 : new Ionic.Crc.CRC32().GetCrc32(mpqEntryStream); var actual = reader.ReadInt32(); if (actual != expected) { return(false); } } } // DateTime if ((dataFlags & 0x2) != 0) { foreach (var mpqEntry in archive) { var dateTime = new DateTime(reader.ReadInt64(), DateTimeKind.Unspecified); } } if (attributesStream.Position < attributesStream.Length) { throw new InvalidDataException(); } } return(true); }
public void SaveTo(Stream stream, MpqArchiveCreateOptions createOptions, bool leaveOpen = false) { if (createOptions is null) { throw new ArgumentNullException(nameof(createOptions)); } if (!createOptions.ListFileCreateMode.HasValue) { createOptions.ListFileCreateMode = _removedFiles.Contains(ListFile.FileName.GetStringHash()) ? MpqFileCreateMode.Prune : MpqFileCreateMode.Overwrite; } if (!createOptions.AttributesCreateMode.HasValue) { createOptions.AttributesCreateMode = _removedFiles.Contains(Attributes.FileName.GetStringHash()) ? MpqFileCreateMode.Prune : MpqFileCreateMode.Overwrite; } createOptions.HashTableSize ??= _originalHashTableSize; MpqArchive.Create(stream, GetMpqFiles().ToArray(), createOptions, leaveOpen).Dispose(); }
public static IEnumerable <(string fileName, MpqLocale locale, Stream stream)> EnumerateFiles(string path) { if (File.Exists(path)) { // Assume file at path is an mpq archive. var archive = MpqArchive.Open(path); var listfile = archive.OpenFile(ListFile.Key); using (var reader = new StreamReader(listfile)) { while (!reader.EndOfStream) { var fileName = reader.ReadLine(); var memoryStream = new MemoryStream(); archive.OpenFile(fileName).CopyTo(memoryStream); memoryStream.Position = 0; yield return(fileName, MpqLocale.Neutral, memoryStream); } } archive.Dispose(); } else if (Directory.Exists(path)) { var pathPrefixLength = path.Length + (path.EndsWith(@"\", StringComparison.Ordinal) ? 0 : 1); foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) { var fileName = new FileInfo(file).ToString().Substring(pathPrefixLength); // var memoryStream = new MemoryStream(); // File.OpenRead(file).CopyTo(memoryStream); var locale = MpqLocaleProvider.GetPathLocale(fileName, out var filePath); yield return(filePath, locale, File.OpenRead(file)); } } }
protected override void GetTableEntries(MpqArchive mpqArchive, uint index, uint relativeFileOffset, uint compressedSize, uint fileSize, out MpqEntry mpqEntry, out MpqHash mpqHash) { throw new NotSupportedException(); }
/// <summary> /// Initializes a new instance of the <see cref="MpqStream"/> class. /// </summary> /// <param name="archive">The archive from which to load a file.</param> /// <param name="entry">The file's entry in the <see cref="BlockTable"/>.</param> internal MpqStream(MpqArchive archive, MpqEntry entry) : this(entry, archive.BaseStream, archive.BlockSize) { }
/// <exception cref="FileNotFoundException"></exception> public static Stream GetFile(MpqArchive archive, string path) { return(MpqFile.OpenRead(archive, path)); }
public static bool FileExists(MpqArchive archive, string path) { return(MpqFile.Exists(archive, path)); }
public CustomMpqArchiveBuilder(MpqArchive originalMpqArchive) : base(originalMpqArchive) { }
protected override void GetTableEntries(MpqArchive mpqArchive, uint index, uint relativeFileOffset, uint compressedSize, uint fileSize, out MpqEntry mpqEntry, out MpqHash mpqHash) { mpqEntry = new MpqEntry(null, mpqArchive.HeaderOffset, relativeFileOffset, compressedSize, fileSize, TargetFlags); mpqHash = new MpqHash(Name, Locale, index, Mask); }