Beispiel #1
0
 /// <summary>
 /// Tries to return the compressed Stream of the entry.
 /// </summary>
 /// <param name="entryFileType"></param>
 /// <param name="stream"></param>
 /// <returns></returns>
 public bool TryGetEntryFileStream(OMODEntryFileType entryFileType, [MaybeNullWhen(false)] out Stream stream)
 {
     stream = null;
     if (!TryGetArchiveEntry(entryFileType, out var archiveEntry))
     {
         return(false);
     }
     stream = archiveEntry.Open();
     return(true);
 }
Beispiel #2
0
        /// <summary>
        /// Returns the <see cref="ZipArchiveEntry"/> from the archive.
        /// </summary>
        /// <param name="entryFileType"></param>
        /// <returns></returns>
        /// <exception cref="OMODEntryNotFoundException"></exception>
        public ZipArchiveEntry GetArchiveEntry(OMODEntryFileType entryFileType)
        {
            var entry = _zipArchive.GetEntry(entryFileType.ToFileString());

            if (entry == null)
            {
                throw new OMODEntryNotFoundException(entryFileType);
            }

            return(entry);
        }
Beispiel #3
0
        /// <summary>
        /// Returns the decompressed entry as a <see cref="MemoryStream"/>.
        /// </summary>
        /// <param name="entryFileType"></param>
        /// <returns></returns>
        public MemoryStream GetDecompressedEntryFileStream(OMODEntryFileType entryFileType)
        {
            var entry = GetArchiveEntry(entryFileType);

            using var stream = entry.Open();
            var ms = new MemoryStream((int)entry.Length);

            stream.CopyTo(ms);
            ms.Position = 0;
            return(ms);
        }
Beispiel #4
0
        /// <summary>
        /// Extracts the entry to disk asynchronous.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="entryFileType"></param>
        /// <param name="cancellationToken"></param>
        public async Task ExtractEntryFileAsync(string path, OMODEntryFileType entryFileType, CancellationToken?cancellationToken = null)
        {
            var stream = GetEntryFileStream(entryFileType);

            await using (stream.ConfigureAwait(false))
            {
                var fs = File.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
                await using (fs.ConfigureAwait(false))
                {
                    await stream.CopyToAsync(fs, cancellationToken ?? CancellationToken.None).ConfigureAwait(false);
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Converts a <see cref="OMODEntryFileType"/> to a string.
 /// </summary>
 /// <param name="entryFileType">The entry file type to convert.</param>
 /// <returns></returns>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 public static string ToFileString(this OMODEntryFileType entryFileType)
 {
     return(entryFileType switch
     {
         OMODEntryFileType.Config => "config",
         OMODEntryFileType.Readme => "readme",
         OMODEntryFileType.Script => "script",
         OMODEntryFileType.Image => "image",
         OMODEntryFileType.Data => "data",
         OMODEntryFileType.DataCRC => "data.crc",
         OMODEntryFileType.Plugins => "plugins",
         OMODEntryFileType.PluginsCRC => "plugins.crc",
         _ => throw new ArgumentOutOfRangeException(nameof(entryFileType), entryFileType, null)
     });
Beispiel #6
0
 internal OMODEntryNotFoundException(OMODEntryFileType entryFileType) : base($"OMOD does not contain a {entryFileType.ToFileString()} file")
 {
 }
Beispiel #7
0
        /// <summary>
        /// Determines if the <see cref="OMODEntryFileType"/> is present in the archive.
        /// </summary>
        /// <param name="entryFileType"></param>
        /// <returns></returns>
        public bool HasEntryFile(OMODEntryFileType entryFileType)
        {
            var entry = _zipArchive.GetEntry(entryFileType.ToFileString());

            return(entry != null);
        }
Beispiel #8
0
 private string GetStringFromEntryFile(OMODEntryFileType entryFileType)
 {
     using var stream = GetEntryFileStream(entryFileType);
     using var br     = new BinaryReader(stream);
     return(br.ReadString());
 }
Beispiel #9
0
 /// <summary>
 /// Extracts the entry to disk.
 /// </summary>
 /// <param name="path"></param>
 /// <param name="entryFileType"></param>
 public void ExtractEntryFile(string path, OMODEntryFileType entryFileType)
 {
     using var stream = GetEntryFileStream(entryFileType);
     using var fs     = File.Open(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
     stream.CopyTo(fs);
 }
Beispiel #10
0
 /// <summary>
 /// Returns the compressed Stream of the entry.
 /// </summary>
 /// <param name="entryFileType"></param>
 /// <returns></returns>
 public Stream GetEntryFileStream(OMODEntryFileType entryFileType)
 {
     return(GetArchiveEntry(entryFileType).Open());
 }
Beispiel #11
0
 /// <summary>
 /// Tries to return the <see cref="ZipArchiveEntry"/> from the archive.
 /// </summary>
 /// <param name="entryFileType"></param>
 /// <param name="archiveEntry"></param>
 /// <returns></returns>
 public bool TryGetArchiveEntry(OMODEntryFileType entryFileType,
                                [MaybeNullWhen(false)] out ZipArchiveEntry archiveEntry)
 {
     archiveEntry = _zipArchive.GetEntry(entryFileType.ToFileString());
     return(archiveEntry != null);
 }