Browse Source

Update language to C# 8

ghorsington 4 năm trước cách đây
mục cha
commit
735575a6a5

+ 4 - 3
ArcToolkitCLI/App.config

@@ -1,6 +1,7 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
+
 <configuration>
   <startup>
-    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
+    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
   </startup>
-</configuration>
+</configuration>

+ 1 - 0
ArcToolkitCLI/ArcToolkitCLI.csproj

@@ -34,6 +34,7 @@
     <DefineConstants>TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <LangVersion>8.0</LangVersion>
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="COM3D2.Toolkit">

+ 5 - 4
ArcToolkitCLI/Commands/ConvertCommand.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Linq;
 using ArcToolkitCLI.Commands.Converters;
 using CommandLine;
@@ -12,14 +11,16 @@ namespace ArcToolkitCLI.Commands
         public int Run()
         {
             var commandTypes = typeof(ConvertCommand).Assembly.GetTypes()
-                .Where(t => !t.IsInterface && !t.IsAbstract && typeof(IConverterCommand).IsAssignableFrom(t)).ToArray();
+                                                     .Where(t => !t.IsInterface && !t.IsAbstract &&
+                                                                 typeof(IConverterCommand).IsAssignableFrom(t))
+                                                     .ToArray();
 
             return new Parser(with =>
                 {
                     with.IgnoreUnknownArguments = true;
                     with.HelpWriter = Parser.Default.Settings.HelpWriter;
                 }).ParseArguments(Environment.GetCommandLineArgs().Skip(2), commandTypes)
-                .MapResult((object t) => ((IConverterCommand)t).Run(), errs => 1);
+                  .MapResult((object t) => ((IConverterCommand)t).Run(), errs => 1);
         }
     }
-}
+}

+ 3 - 9
ArcToolkitCLI/Commands/Converters/IConverterCommand.cs

@@ -1,13 +1,7 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace ArcToolkitCLI.Commands.Converters
+namespace ArcToolkitCLI.Commands.Converters
 {
-    interface IConverterCommand
+    internal interface IConverterCommand
     {
         int Run();
     }
-}
+}

+ 82 - 71
ArcToolkitCLI/Commands/Converters/NeiConverter.cs

@@ -13,9 +13,33 @@ namespace ArcToolkitCLI.Commands.Converters
     internal class NeiConverter : IConverterCommand, IInputOptions, IOutputOptions
     {
         private static readonly byte[] NEI_KEY =
-            {0xAA, 0xC9, 0xD2, 0x35, 0x22, 0x87, 0x20, 0xF2, 0x40, 0xC5, 0x61, 0x7C, 0x01, 0xDF, 0x66, 0x54};
+        {
+            0xAA,
+            0xC9,
+            0xD2,
+            0x35,
+            0x22,
+            0x87,
+            0x20,
+            0xF2,
+            0x40,
+            0xC5,
+            0x61,
+            0x7C,
+            0x01,
+            0xDF,
+            0x66,
+            0x54
+        };
+
+        private static readonly byte[] NEI_MAGIC =
+        {
+            0x77,
+            0x73,
+            0x76,
+            0xFF
+        };
 
-        private static readonly byte[] NEI_MAGIC = {0x77, 0x73, 0x76, 0xFF};
         private static readonly Encoding ShiftJisEncoding = Encoding.GetEncoding(932);
 
         [Option('s', "separator", Default = ';', HelpText = "Value separator of the CSV file")]
@@ -71,7 +95,7 @@ namespace ArcToolkitCLI.Commands.Converters
             int nextChar;
             while ((nextChar = tr.Read()) != -1)
             {
-                var c = (char) nextChar;
+                var c = (char)nextChar;
 
                 if (c == '\n' && !isQuoted)
                 {
@@ -156,9 +180,7 @@ namespace ArcToolkitCLI.Commands.Converters
 
             List<List<string>> values;
             using (var tr = File.OpenText(filePath))
-            {
                 values = ParseCSV(tr);
-            }
 
             var cols = values.Max(l => l.Count);
             var rows = values.Count;
@@ -174,42 +196,37 @@ namespace ArcToolkitCLI.Commands.Converters
                         : new byte[0];
             }
 
-            using (var ms = new MemoryStream())
-            {
-                using (var bw = new BinaryWriter(ms))
-                {
-                    bw.Write(NEI_MAGIC);
-                    bw.Write(cols);
-                    bw.Write(rows);
-
-                    var totalLength = 0;
-                    foreach (var encodedValue in encodedValues)
-                    {
-                        var len = encodedValue.Length;
-                        if (len != 0)
-                            len++;
+            using var ms = new MemoryStream();
+            using var bw = new BinaryWriter(ms);
+            bw.Write(NEI_MAGIC);
+            bw.Write(cols);
+            bw.Write(rows);
 
-                        bw.Write(encodedValue.Length == 0 ? 0 : totalLength);
-                        bw.Write(len);
-                        totalLength += len;
-                    }
+            var totalLength = 0;
+            foreach (var encodedValue in encodedValues)
+            {
+                var len = encodedValue.Length;
+                if (len != 0)
+                    len++;
 
-                    for (var i = 0; i < encodedValues.Length; i++)
-                    {
-                        var encodedValue = encodedValues[i];
-                        if (encodedValue.Length == 0)
-                            continue;
+                bw.Write(encodedValue.Length == 0 ? 0 : totalLength);
+                bw.Write(len);
+                totalLength += len;
+            }
 
-                        bw.Write(encodedValue);
-                        if (i != encodedValue.Length - 1)
-                            bw.Write((byte) 0x00);
-                    }
-                }
+            for (var i = 0; i < encodedValues.Length; i++)
+            {
+                var encodedValue = encodedValues[i];
+                if (encodedValue.Length == 0)
+                    continue;
 
-                var data = ms.ToArray();
-                File.WriteAllBytes(Path.Combine(Output, $"{nameNoExt}.nei"), Encryption.EncryptBytes(data, NEI_KEY));
+                bw.Write(encodedValue);
+                if (i != encodedValue.Length - 1)
+                    bw.Write((byte)0x00);
             }
 
+            var data = ms.ToArray();
+            File.WriteAllBytes(Path.Combine(Output, $"{nameNoExt}.nei"), Encryption.EncryptBytes(data, NEI_KEY));
             return 0;
         }
 
@@ -226,50 +243,44 @@ namespace ArcToolkitCLI.Commands.Converters
 
             var neiData = Encryption.DecryptBytes(File.ReadAllBytes(filePath), NEI_KEY);
 
-            using (var ms = new MemoryStream(neiData))
+            using var ms = new MemoryStream(neiData);
+            using var br = new BinaryReader(ms);
+            if (!br.ReadBytes(4).SequenceEqual(NEI_MAGIC))
             {
-                using (var br = new BinaryReader(ms))
-                {
-                    if (!br.ReadBytes(4).SequenceEqual(NEI_MAGIC))
-                    {
-                        Console.WriteLine($"File {filePath} is not a valid NEI file");
-                        return 1;
-                    }
+                Console.WriteLine($"File {filePath} is not a valid NEI file");
+                return 1;
+            }
 
-                    var cols = br.ReadUInt32();
-                    var rows = br.ReadUInt32();
+            var cols = br.ReadUInt32();
+            var rows = br.ReadUInt32();
 
-                    var strLengths = new int[cols * rows];
+            var strLengths = new int[cols * rows];
 
-                    for (var cell = 0; cell < cols * rows; cell++)
-                    {
-                        br.ReadInt32(); // Total length of all strings because why not
-                        strLengths[cell] = br.ReadInt32();
-                    }
+            for (var cell = 0; cell < cols * rows; cell++)
+            {
+                br.ReadInt32(); // Total length of all strings because why not
+                strLengths[cell] = br.ReadInt32();
+            }
 
-                    var values = new string[cols * rows];
+            var values = new string[cols * rows];
 
-                    for (var cell = 0; cell < cols * rows; cell++)
-                    {
-                        var len = strLengths[cell];
-                        values[cell] = ShiftJisEncoding.GetString(br.ReadBytes(len), 0, Math.Max(len - 1, 0));
-                    }
+            for (var cell = 0; cell < cols * rows; cell++)
+            {
+                var len = strLengths[cell];
+                values[cell] = ShiftJisEncoding.GetString(br.ReadBytes(len), 0, Math.Max(len - 1, 0));
+            }
 
-                    using (var tw = File.CreateText(Path.Combine(Output, $"{nameNoExt}.csv")))
-                    {
-                        for (var row = 0; row < rows; row++)
-                        {
-                            for (var col = 0; col < cols; col++)
-                            {
-                                tw.Write(Escape(values[row * cols + col]));
-                                if (col != cols - 1)
-                                    tw.Write(ValueSeparator);
-                            }
-
-                            tw.WriteLine();
-                        }
-                    }
+            using var tw = File.CreateText(Path.Combine(Output, $"{nameNoExt}.csv"));
+            for (var row = 0; row < rows; row++)
+            {
+                for (var col = 0; col < cols; col++)
+                {
+                    tw.Write(Escape(values[row * cols + col]));
+                    if (col != cols - 1)
+                        tw.Write(ValueSeparator);
                 }
+
+                tw.WriteLine();
             }
 
             return 0;

+ 62 - 66
ArcToolkitCLI/Commands/Converters/TexConverter.cs

@@ -17,7 +17,18 @@ namespace ArcToolkitCLI.Commands.Converters
     {
         private const string TEX_TAG = "CM3D2_TEX";
         private const int TEX_VERSION = 1010;
-        private readonly byte[] PNG_HEADER = {137, 80, 78, 71, 13, 10, 26, 10};
+
+        private readonly byte[] PNG_HEADER =
+        {
+            137,
+            80,
+            78,
+            71,
+            13,
+            10,
+            26,
+            10
+        };
 
         private readonly Dictionary<TextureFormat, Action<string, byte[], int, int, TextureFormat>> textureLoaders;
 
@@ -94,73 +105,66 @@ namespace ArcToolkitCLI.Commands.Converters
 
             var w = BitConverter.ToInt32(img, PNG_HEADER.Length + 4 + 4);
             var h = BitConverter.ToInt32(img, PNG_HEADER.Length + 4 + 4 + 4);
-            using (var bw =
-                new BinaryWriter(File.Create(Path.Combine(Output, $"{Path.GetFileNameWithoutExtension(file)}.tex"))))
-            {
-                bw.Write(TEX_TAG);
-                bw.Write(TEX_VERSION);
-                bw.Write(string.Empty);
-                bw.Write(w);
-                bw.Write(h);
-                bw.Write((int) TextureFormat.ARGB32);
-                bw.Write(img.Length);
-                bw.Write(img);
-            }
-
+            using var bw =
+                new BinaryWriter(File.Create(Path.Combine(Output, $"{Path.GetFileNameWithoutExtension(file)}.tex")));
+            bw.Write(TEX_TAG);
+            bw.Write(TEX_VERSION);
+            bw.Write(string.Empty);
+            bw.Write(w);
+            bw.Write(h);
+            bw.Write((int)TextureFormat.ARGB32);
+            bw.Write(img.Length);
+            bw.Write(img);
             return 0;
         }
 
         private int TexToPng(string file)
         {
-            using (var br = new BinaryReader(File.OpenRead(file)))
-            {
-                var tag = br.ReadString();
+            using var br = new BinaryReader(File.OpenRead(file));
+            var tag = br.ReadString();
 
-                if (tag != TEX_TAG)
-                {
-                    Console.WriteLine($"File {file} is not a valid TEX file!");
-                    return 1;
-                }
+            if (tag != TEX_TAG)
+            {
+                Console.WriteLine($"File {file} is not a valid TEX file!");
+                return 1;
+            }
 
-                var version = br.ReadInt32();
-                br.ReadString();
-                var width = 0;
-                var height = 0;
+            var version = br.ReadInt32();
+            br.ReadString();
+            var width = 0;
+            var height = 0;
 
-                var format = TextureFormat.ARGB32;
+            var format = TextureFormat.ARGB32;
 
-                if (version >= 1010)
-                {
-                    width = br.ReadInt32();
-                    height = br.ReadInt32();
-                    format = (TextureFormat) br.ReadInt32();
-                }
+            if (version >= 1010)
+            {
+                width = br.ReadInt32();
+                height = br.ReadInt32();
+                format = (TextureFormat)br.ReadInt32();
+            }
 
-                if (!Enum.IsDefined(typeof(TextureFormat), format))
-                {
-                    Console.WriteLine($"File {file} has unsupported texture format: {format}");
-                    return 1;
-                }
+            if (!Enum.IsDefined(typeof(TextureFormat), format))
+            {
+                Console.WriteLine($"File {file} has unsupported texture format: {format}");
+                return 1;
+            }
 
-                var size = br.ReadInt32();
-                var data = new byte[size];
-                br.Read(data, 0, size);
+            var size = br.ReadInt32();
+            var data = new byte[size];
+            br.Read(data, 0, size);
 
-                if (version == 1000)
-                {
-                    width = BitConverter.ToInt32(data, PNG_HEADER.Length + 4 + 4);
-                    height = BitConverter.ToInt32(data, PNG_HEADER.Length + 4 + 4 + 4);
-                }
+            if (version == 1000)
+            {
+                width = BitConverter.ToInt32(data, PNG_HEADER.Length + 4 + 4);
+                height = BitConverter.ToInt32(data, PNG_HEADER.Length + 4 + 4 + 4);
+            }
 
-                if (textureLoaders.TryGetValue(format, out var saveTex))
-                {
-                    saveTex(file, data, width, height, format);
-                }
-                else
-                {
-                    Console.WriteLine($"File {file} uses format {format} that is not supported!");
-                    return 1;
-                }
+            if (textureLoaders.TryGetValue(format, out var saveTex))
+                saveTex(file, data, width, height, format);
+            else
+            {
+                Console.WriteLine($"File {file} uses format {format} that is not supported!");
+                return 1;
             }
 
             return 0;
@@ -177,17 +181,9 @@ namespace ArcToolkitCLI.Commands.Converters
 
         private void ConvertFromDxt(string file, byte[] data, int width, int height, TextureFormat format)
         {
-            var squishFlags = (SquishFlags) 0;
-
-            switch (format)
-            {
-                case TextureFormat.DXT1:
-                    squishFlags |= SquishFlags.kDxt1;
-                    break;
-                case TextureFormat.DXT5:
-                    squishFlags |= SquishFlags.kDxt5;
-                    break;
-            }
+            var squishFlags =
+                format switch { TextureFormat.DXT1 => SquishFlags.kDxt1, TextureFormat.DXT5 => SquishFlags.kDxt5, _ =>
+                    throw new ArgumentException($"Invalid DXT texture format: {format}", nameof(format)) };
 
             var outData = new byte[width * height * 4];
             Squish.Squish.DecompressImage(outData, width, height, ref data, squishFlags);

+ 32 - 37
ArcToolkitCLI/Commands/DecryptCommand.cs

@@ -25,45 +25,40 @@ namespace ArcToolkitCLI.Commands
                 if (!file.Exists)
                     return Errors.Error(Errors.ErrorCodes.NotAFile, file.Name);
 
-                using (var stream = File.OpenRead(file.FullName))
-                {
-                    var header = new byte[4];
-                    stream.Read(header, 0, header.Length);
-                    var headerString = Encoding.ASCII.GetString(header);
-                    stream.Position = 0;
-                    if (headerString == "warc")
-                        Console.WriteLine($"{file.Name} is a WARC file, skipping...");
-                    else if (headerString == "warp")
-                        try
+                using var stream = File.OpenRead(file.FullName);
+                var header = new byte[4];
+                stream.Read(header, 0, header.Length);
+                var headerString = Encoding.ASCII.GetString(header);
+                stream.Position = 0;
+                if (headerString == "warc")
+                    Console.WriteLine($"{file.Name} is a WARC file, skipping...");
+                else if (headerString == "warp")
+                    try
+                    {
+                        var arcStream = WarpArc.DecryptWarp(stream, requestedFile =>
                         {
-                            var arcStream = WarpArc.DecryptWarp(stream, requestedFile =>
-                            {
-                                if (keysDict.TryGetValue("*", out var key))
-                                    return key;
-                                if (keysDict.TryGetValue(requestedFile, out key))
-                                    return key;
-                                if (Directory.Exists(ArcDirectory) &&
-                                    File.Exists(Path.Combine(ArcDirectory, requestedFile)))
-                                    return Encryption.ReadKeyFromFile(Path.Combine(ArcDirectory, requestedFile));
-                                if (File.Exists(WarcFile))
-                                    return Encryption.ReadKeyFromFile(WarcFile);
-                                throw new FileNotFoundException("No key found for the requested ARC", requestedFile);
-                            });
+                            if (keysDict.TryGetValue("*", out var key))
+                                return key;
+                            if (keysDict.TryGetValue(requestedFile, out key))
+                                return key;
+                            if (Directory.Exists(ArcDirectory) &&
+                                File.Exists(Path.Combine(ArcDirectory, requestedFile)))
+                                return Encryption.ReadKeyFromFile(Path.Combine(ArcDirectory, requestedFile));
+                            if (File.Exists(WarcFile))
+                                return Encryption.ReadKeyFromFile(WarcFile);
+                            throw new FileNotFoundException("No key found for the requested ARC", requestedFile);
+                        });
 
-                            Console.WriteLine(
-                                $"Writing {Path.Combine(Output, file.Name)} with length {arcStream.Length}");
-                            using (var output = File.Create(Path.Combine(Output, file.Name)))
-                            {
-                                arcStream.CopyTo(output);
-                            }
-                        }
-                        catch (FileNotFoundException fe)
-                        {
-                            return Errors.Error(Errors.ErrorCodes.NoCorrectKey, file.Name, fe.FileName);
-                        }
-                    else
-                        return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
-                }
+                        Console.WriteLine($"Writing {Path.Combine(Output, file.Name)} with length {arcStream.Length}");
+                        using (var output = File.Create(Path.Combine(Output, file.Name)))
+                            arcStream.CopyTo(output);
+                    }
+                    catch (FileNotFoundException fe)
+                    {
+                        return Errors.Error(Errors.ErrorCodes.NoCorrectKey, file.Name, fe.FileName);
+                    }
+                else
+                    return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
             }
 
             return 0;

+ 58 - 62
ArcToolkitCLI/Commands/ExtractCommand.cs

@@ -1,103 +1,99 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
-using System.Net;
 using System.Text;
 using ArcToolkitCLI.Commands.Options;
 using ArcToolkitCLI.Util;
 using COM3D2.Toolkit.Arc;
 using COM3D2.Toolkit.Arc.Files;
 using CommandLine;
-using GlobExpressions;
-using Glob = ArcToolkitCLI.Util.Glob;
+using Glob = GlobExpressions.Glob;
 
 namespace ArcToolkitCLI.Commands
 {
     [Verb("extract", HelpText = "Extract the contents of the given ARC files")]
     public class ExtractCommand : ICommand, IInputOptions, IDecryptionOptions, IOutputOptions
     {
+        private Glob extractGlob;
+
+        [Option('s', "skip-errors", Required = false,
+            HelpText = "If specified, skips ARC files that failed to extract.", Default = false)]
+        public bool SkipErrors { get; set; }
+
+        [Option('p', "pattern", Required = false, HelpText = "Pattern to select which files to extract", Default = "*")]
+        public string ExtractFilePattern { get; set; }
+
+        private Glob ExtractGlob => extractGlob ?? (extractGlob = new Glob(ExtractFilePattern));
+
         public int Run()
         {
             Dictionary<string, byte[]> keys = null;
 
             Directory.CreateDirectory(Output);
 
-            foreach (var file in Glob.EnumerateFiles(Input))
+            foreach (var file in Util.Glob.EnumerateFiles(Input))
             {
                 if (!file.Exists)
                     return Errors.Error(Errors.ErrorCodes.NotAFile, file.Name);
 
-                using (var stream = File.OpenRead(file.FullName))
-                {
-                    var header = new byte[4];
-                    stream.Read(header, 0, header.Length);
-                    var headerString = Encoding.ASCII.GetString(header);
-                    stream.Position = 0;
-                    WarcArc arc = null;
+                using var stream = File.OpenRead(file.FullName);
+                var header = new byte[4];
+                stream.Read(header, 0, header.Length);
+                var headerString = Encoding.ASCII.GetString(header);
+                stream.Position = 0;
+                WarcArc arc = null;
 
-                    if (headerString == "warp")
+                if (headerString == "warp")
+                {
+                    if (keys == null)
                     {
-                        if (keys == null)
+                        var err = Encryption.GetDecryptionKeys(this, out keys);
+                        if (err != 0)
                         {
-                            var err = Encryption.GetDecryptionKeys(this, out keys);
-                            if (err != 0)
-                            {
-                                if(SkipErrors)
-                                    continue;
-                                return err;
-                            }
-
-                            arc = new WarpArc(stream, requestedFile =>
-                            {
-                                if (keys.TryGetValue("*", out var key))
-                                    return key;
-                                if (keys.TryGetValue(requestedFile, out key))
-                                    return key;
-                                if (Directory.Exists(ArcDirectory) &&
-                                    File.Exists(Path.Combine(ArcDirectory, requestedFile)))
-                                    return Encryption.ReadKeyFromFile(Path.Combine(ArcDirectory, requestedFile));
-                                if (File.Exists(WarcFile))
-                                    return Encryption.ReadKeyFromFile(WarcFile);
-                                throw new FileNotFoundException("No key found for the requested ARC", requestedFile);
-                            });
+                            if (SkipErrors)
+                                continue;
+                            return err;
                         }
-                    }
-                    else if (headerString == "warc")
-                    {
-                        arc = new WarcArc(stream);
-                    }
 
-                    if (arc == null)
-                    {
-                        var err = Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
-                        if (SkipErrors)
-                            continue;
-                        return err;
+                        arc = new WarpArc(stream, requestedFile =>
+                        {
+                            if (keys.TryGetValue("*", out var key))
+                                return key;
+                            if (keys.TryGetValue(requestedFile, out key))
+                                return key;
+                            if (Directory.Exists(ArcDirectory) &&
+                                File.Exists(Path.Combine(ArcDirectory, requestedFile)))
+                                return Encryption.ReadKeyFromFile(Path.Combine(ArcDirectory, requestedFile));
+                            if (File.Exists(WarcFile))
+                                return Encryption.ReadKeyFromFile(WarcFile);
+                            throw new FileNotFoundException("No key found for the requested ARC", requestedFile);
+                        });
                     }
+                }
+                else if (headerString == "warc")
+                    arc = new WarcArc(stream);
+
+                if (arc == null)
+                {
+                    var err = Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
+                    if (SkipErrors)
+                        continue;
+                    return err;
+                }
 
-                    Console.WriteLine($"Extracting {arc.Name}");
-                    var arcDir = Path.Combine(Output, Path.GetFileNameWithoutExtension(arc.Name));
+                Console.WriteLine($"Extracting {arc.Name}");
+                var arcDir = Path.Combine(Output, Path.GetFileNameWithoutExtension(arc.Name));
 
-                    Directory.CreateDirectory(arcDir);
-                    foreach (var arcEntry in arc.Entries)
-                        ExtractArchive(arcEntry, arcDir);
+                Directory.CreateDirectory(arcDir);
+                foreach (var arcEntry in arc.Entries)
+                    ExtractArchive(arcEntry, arcDir);
 
-                    arc.Dispose();
-                }
+                arc.Dispose();
             }
 
             return 0;
         }
 
-        [Option('s', "skip-errors", Required = false, HelpText = "If specified, skips ARC files that failed to extract.", Default = false)]
-        public bool SkipErrors { get; set; }
-
-        [Option('p',"pattern", Required = false, HelpText = "Pattern to select which files to extract", Default = "*")]
-        public string ExtractFilePattern { get; set; }
-
-        private GlobExpressions.Glob extractGlob = null;
-        private GlobExpressions.Glob ExtractGlob => extractGlob ?? (extractGlob = new GlobExpressions.Glob(ExtractFilePattern));
-
         public string ArcDirectory { get; set; }
         public string DecryptionKey { get; set; }
         public string KeyFile { get; set; }
@@ -118,8 +114,8 @@ namespace ArcToolkitCLI.Commands
                     Console.WriteLine(fullName);
                     Directory.CreateDirectory(Path.GetDirectoryName(fullName));
                     using (var file = File.Create(fullName))
-                    using (var data = fe.GetDataStream())
                     {
+                        using var data = fe.GetDataStream();
                         data.CopyTo(file);
                     }
 

+ 33 - 39
ArcToolkitCLI/Commands/InfoCommand.cs

@@ -30,52 +30,46 @@ namespace ArcToolkitCLI.Commands
                     Console.WriteLine();
                 first = false;
 
-                using (var stream = File.OpenRead(file.FullName))
-                {
-                    var header = new byte[4];
-                    stream.Read(header, 0, header.Length);
-                    var headerString = Encoding.ASCII.GetString(header);
-                    stream.Position = 0;
+                using var stream = File.OpenRead(file.FullName);
+                var header = new byte[4];
+                stream.Read(header, 0, header.Length);
+                var headerString = Encoding.ASCII.GetString(header);
+                stream.Position = 0;
 
-                    if (headerString == "warc")
+                if (headerString == "warc")
+                {
+                    var key = new byte[2048];
+                    stream.Read(key, 0, key.Length);
+                    var keyBase64 = Convert.ToBase64String(key);
+                    if (OnlyKey)
                     {
-                        var key = new byte[2048];
-                        stream.Read(key, 0, key.Length);
-                        var keyBase64 = Convert.ToBase64String(key);
-                        if (OnlyKey)
-                        {
-                            Console.WriteLine($"{Path.GetFileName(file.FullName)}:{keyBase64}");
-                            continue;
-                        }
-
-                        stream.Position = 0;
-                        using (var warc = new WarcArc(stream))
-                        {
-                            Console.WriteLine($"File name: {file.Name}");
-                            Console.WriteLine("ARC type: WARC");
-                            Console.WriteLine($"ARC Name: {warc.Name}");
-                            Console.WriteLine($"File count: {warc.Entries.Count()}");
-                            Console.WriteLine($"Decryption key: {keyBase64}");
-                        }
+                        Console.WriteLine($"{Path.GetFileName(file.FullName)}:{keyBase64}");
+                        continue;
                     }
-                    else if (headerString == "warp")
-                    {
-                        if (OnlyKey)
-                            continue;
 
-                        Console.WriteLine($"File name: {file.Name}");
-                        Console.WriteLine("ARC type: WARP");
-                        using (var br = new BinaryReader(stream))
-                        {
-                            Console.WriteLine(
-                                $"Needs a decryption key from the following ARC: {WarpArc.GetKeyWarpName(br)}");
-                        }
-                    }
-                    else
+                    stream.Position = 0;
+                    using (var warc = new WarcArc(stream))
                     {
-                        return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
+                        Console.WriteLine($"File name: {file.Name}");
+                        Console.WriteLine("ARC type: WARC");
+                        Console.WriteLine($"ARC Name: {warc.Name}");
+                        Console.WriteLine($"File count: {warc.Entries.Count()}");
+                        Console.WriteLine($"Decryption key: {keyBase64}");
                     }
                 }
+                else if (headerString == "warp")
+                {
+                    if (OnlyKey)
+                        continue;
+
+                    Console.WriteLine($"File name: {file.Name}");
+                    Console.WriteLine("ARC type: WARP");
+                    using (var br = new BinaryReader(stream))
+                        Console.WriteLine(
+                            $"Needs a decryption key from the following ARC: {WarpArc.GetKeyWarpName(br)}");
+                }
+                else
+                    return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
             }
 
             return 0;

+ 7 - 7
ArcToolkitCLI/Program.cs

@@ -9,14 +9,14 @@ namespace ArcToolkitCLI
         private static int Main(string[] args)
         {
             var commandTypes = typeof(Program).Assembly.GetTypes()
-                .Where(t => !t.IsInterface && !t.IsAbstract && typeof(ICommand).IsAssignableFrom(t)).ToArray();
+                                              .Where(t => !t.IsInterface && !t.IsAbstract &&
+                                                          typeof(ICommand).IsAssignableFrom(t)).ToArray();
             return new Parser(with =>
-                {
-                    with.IgnoreUnknownArguments = true;
-                    with.AutoHelp = true;
-                    with.HelpWriter = Parser.Default.Settings.HelpWriter;
-                }).ParseArguments(args, commandTypes)
-                .MapResult((object t) => ((ICommand) t).Run(), errs => 1);
+            {
+                with.IgnoreUnknownArguments = true;
+                with.AutoHelp = true;
+                with.HelpWriter = Parser.Default.Settings.HelpWriter;
+            }).ParseArguments(args, commandTypes).MapResult((object t) => ((ICommand)t).Run(), errs => 1);
         }
     }
 }

+ 35 - 43
ArcToolkitCLI/Util/Encryption.cs

@@ -12,10 +12,8 @@ namespace ArcToolkitCLI.Util
 
         public static byte[] ReadKeyFromFile(string filename)
         {
-            using (var br = new BinaryReader(File.OpenRead(filename)))
-            {
-                return br.ReadBytes(2048);
-            }
+            using var br = new BinaryReader(File.OpenRead(filename));
+            return br.ReadBytes(2048);
         }
 
         public static int GetDecryptionKeys(IDecryptionOptions opts, out Dictionary<string, byte[]> keys)
@@ -35,28 +33,30 @@ namespace ArcToolkitCLI.Util
                 }
             }
             else if (!string.IsNullOrWhiteSpace(opts.DecryptionKey))
-            {
                 keys["*"] = Convert.FromBase64String(opts.DecryptionKey);
-            }
             else if (string.IsNullOrWhiteSpace(opts.ArcDirectory) && string.IsNullOrWhiteSpace(opts.WarcFile))
-            {
                 return Errors.Error(Errors.ErrorCodes.KeyNotFound);
-            }
 
             return 0;
         }
 
         public static byte[] GenerateIV(byte[] ivSeed)
         {
-            uint[] seed = {0x075BCD15, 0x159A55E5, 0x1F123BB5, BitConverter.ToUInt32(ivSeed, 0) ^ 0xBFBFBFBF};
+            uint[] seed =
+            {
+                0x075BCD15,
+                0x159A55E5,
+                0x1F123BB5,
+                BitConverter.ToUInt32(ivSeed, 0) ^ 0xBFBFBFBF
+            };
 
             for (var i = 0; i < 4; i++)
             {
-                var n = seed[0] ^ (seed[0] << 11);
+                var n = seed[0] ^ seed[0] << 11;
                 seed[0] = seed[1];
                 seed[1] = seed[2];
                 seed[2] = seed[3];
-                seed[3] = n ^ seed[3] ^ ((n ^ (seed[3] >> 11)) >> 8);
+                seed[3] = n ^ seed[3] ^ (n ^ seed[3] >> 11) >> 8;
             }
 
             var output = new byte[16];
@@ -74,21 +74,17 @@ namespace ArcToolkitCLI.Util
 
             var iv = GenerateIV(ivSeed);
 
-            using (var rijndael = new RijndaelManaged())
-            {
-                rijndael.Padding = PaddingMode.None;
+            using var rijndael = new RijndaelManaged();
+            rijndael.Padding = PaddingMode.None;
 
-                using (var decryptor = rijndael.CreateDecryptor(key, iv))
-                using (var mem = new MemoryStream(encryptedBytes, 0, encryptedBytes.Length - 5))
-                using (var stream = new CryptoStream(mem, decryptor, CryptoStreamMode.Read))
-                {
-                    var output = new byte[encryptedBytes.Length - extraDataSize - 5];
+            using var decryptor = rijndael.CreateDecryptor(key, iv);
+            using var mem = new MemoryStream(encryptedBytes, 0, encryptedBytes.Length - 5);
+            using var stream = new CryptoStream(mem, decryptor, CryptoStreamMode.Read);
+            var output = new byte[encryptedBytes.Length - extraDataSize - 5];
 
-                    stream.Read(output, 0, output.Length);
+            stream.Read(output, 0, output.Length);
 
-                    return output;
-                }
-            }
+            return output;
         }
 
         public static byte[] EncryptBytes(byte[] data, byte[] key, byte[] ivSeed = null)
@@ -105,28 +101,24 @@ namespace ArcToolkitCLI.Util
                 extraData = new byte[newSize - data.Length];
             }
 
-            using (var rijndael = new RijndaelManaged())
-            {
-                rijndael.Padding = PaddingMode.None;
+            using var rijndael = new RijndaelManaged();
+            rijndael.Padding = PaddingMode.None;
 
-                using (var encryptor = rijndael.CreateEncryptor(key, iv))
-                using (var mem = new MemoryStream())
-                {
-                    using (var stream = new CryptoStream(mem, encryptor, CryptoStreamMode.Write))
-                    {
-                        stream.Write(data, 0, data.Length);
-                        if (extraData != null)
-                            stream.Write(extraData, 0, extraData.Length);
-                        stream.Flush();
-
-                        var extraLength = (byte) (extraData?.Length ?? 0);
-                        mem.Write(new[] {(byte) (extraLength ^ ivSeed[0])}, 0, 1);
-                        mem.Write(ivSeed, 0, ivSeed.Length);
-                    }
-
-                    return mem.ToArray();
-                }
+            using var encryptor = rijndael.CreateEncryptor(key, iv);
+            using var mem = new MemoryStream();
+            using (var stream = new CryptoStream(mem, encryptor, CryptoStreamMode.Write))
+            {
+                stream.Write(data, 0, data.Length);
+                if (extraData != null)
+                    stream.Write(extraData, 0, extraData.Length);
+                stream.Flush();
+
+                var extraLength = (byte)(extraData?.Length ?? 0);
+                mem.Write(new[] { (byte)(extraLength ^ ivSeed[0]) }, 0, 1);
+                mem.Write(ivSeed, 0, ivSeed.Length);
             }
+
+            return mem.ToArray();
         }
     }
 }

+ 10 - 10
ArcToolkitCLI/Util/Errors.cs

@@ -4,15 +4,6 @@ namespace ArcToolkitCLI.Util
 {
     public static class Errors
     {
-        public enum ErrorCodes
-        {
-            NotAFile = 100,
-            UnknownArcType,
-            KeyNotFound,
-            InvalidKeyFile,
-            NoCorrectKey
-        }
-
         public static readonly string[] ErrorMessages =
         {
             "{0} is not a file",
@@ -22,10 +13,19 @@ namespace ArcToolkitCLI.Util
             "The ARC {0} needs a key from {1}"
         };
 
+        public enum ErrorCodes
+        {
+            NotAFile = 100,
+            UnknownArcType,
+            KeyNotFound,
+            InvalidKeyFile,
+            NoCorrectKey
+        }
+
         public static int Error(ErrorCodes code, params object[] args)
         {
             Console.Error.WriteLine(ErrorMessages[code - ErrorCodes.NotAFile], args);
-            return (int) code;
+            return (int)code;
         }
     }
 }

+ 10 - 5
ArcToolkitCLI/Util/Glob.cs

@@ -7,7 +7,15 @@ namespace ArcToolkitCLI.Util
 {
     internal class Glob
     {
-        public static readonly char[] GlobCharacters = {'*', '{', '}', '[', ']', '?'};
+        public static readonly char[] GlobCharacters =
+        {
+            '*',
+            '{',
+            '}',
+            '[',
+            ']',
+            '?'
+        };
 
         public static IEnumerable<FileSystemInfoBase> EnumerateFiles(IEnumerable<string> patterns)
         {
@@ -19,9 +27,6 @@ namespace ArcToolkitCLI.Util
                     yield return new FileInfoWrapper(new FileSystem(), new FileInfo(pattern));
         }
 
-        public static bool IsGlob(string path)
-        {
-            return path.IndexOfAny(GlobCharacters) >= 0;
-        }
+        public static bool IsGlob(string path) { return path.IndexOfAny(GlobCharacters) >= 0; }
     }
 }

+ 1 - 0
ArcToolkitCLI/packages.config

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
+
 <packages>
   <package id="Glob" version="1.1.3" targetFramework="net461" />
   <package id="ILRepack" version="2.0.16" targetFramework="net461" />