Parcourir la source

Change Glob library; add globbing for inside files

ghorsington il y a 4 ans
Parent
commit
f3ca3739d1

+ 11 - 2
ArcToolkitCLI/ArcToolkitCLI.csproj

@@ -42,14 +42,23 @@
     <Reference Include="CommandLine">
       <HintPath>..\lib\CommandLine.dll</HintPath>
     </Reference>
-    <Reference Include="Glob, Version=3.0.0.0, Culture=neutral, PublicKeyToken=2561fd83231d3038, processorArchitecture=MSIL">
-      <HintPath>..\packages\Glob.cs.3.0.27\lib\net40\Glob.dll</HintPath>
+    <Reference Include="Glob, Version=1.1.3.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>..\packages\Glob.1.1.3\lib\net46\Glob.dll</HintPath>
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.IO.Abstractions, Version=3.0.0.0, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
       <HintPath>..\packages\System.IO.Abstractions.3.0.10\lib\net40\System.IO.Abstractions.dll</HintPath>
     </Reference>
+    <Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
+    </Reference>
+    <Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
+    </Reference>
+    <Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Commands\ConvertCommand.cs" />

+ 30 - 6
ArcToolkitCLI/Commands/ExtractCommand.cs

@@ -1,12 +1,15 @@
 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;
 
 namespace ArcToolkitCLI.Commands
 {
@@ -38,8 +41,11 @@ namespace ArcToolkitCLI.Commands
                         {
                             var err = Encryption.GetDecryptionKeys(this, out keys);
                             if (err != 0)
+                            {
+                                if(SkipErrors)
+                                    continue;
                                 return err;
-
+                            }
 
                             arc = new WarpArc(stream, requestedFile =>
                             {
@@ -62,9 +68,14 @@ namespace ArcToolkitCLI.Commands
                     }
 
                     if (arc == null)
-                        return Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
+                    {
+                        var err = Errors.Error(Errors.ErrorCodes.UnknownArcType, file.Name);
+                        if (SkipErrors)
+                            continue;
+                        return err;
+                    }
 
-                    Console.WriteLine($"Extracting {arc.Name}:");
+                    Console.WriteLine($"Extracting {arc.Name}");
                     var arcDir = Path.Combine(Output, Path.GetFileNameWithoutExtension(arc.Name));
 
                     Directory.CreateDirectory(arcDir);
@@ -78,6 +89,15 @@ namespace ArcToolkitCLI.Commands
             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; }
@@ -86,13 +106,17 @@ namespace ArcToolkitCLI.Commands
         public IEnumerable<string> Input { get; set; }
         public string Output { get; set; }
 
-        private void ExtractArchive(ArcEntry entry, string path)
+        private void ExtractArchive(ArcEntry entry, string path, string internalPath = ".")
         {
             switch (entry)
             {
                 case ArcFileEntry fe:
                     var fullName = Path.Combine(path, fe.Name);
+                    var fullInternalName = Path.Combine(internalPath, fe.Name);
+                    if (!ExtractGlob.IsMatch(fullInternalName))
+                        return;
                     Console.WriteLine(fullName);
+                    Directory.CreateDirectory(Path.GetDirectoryName(fullName));
                     using (var file = File.Create(fullName))
                     using (var data = fe.GetDataStream())
                     {
@@ -102,9 +126,9 @@ namespace ArcToolkitCLI.Commands
                     break;
                 case ArcDirectoryEntry de:
                     var subfolderPath = Path.Combine(path, de.Name);
-                    Directory.CreateDirectory(subfolderPath);
+                    var realSubfolderPath = Path.Combine(internalPath, de.Name);
                     foreach (var arcEntry in de.Children)
-                        ExtractArchive(arcEntry, subfolderPath);
+                        ExtractArchive(arcEntry, subfolderPath, realSubfolderPath);
                     break;
             }
         }

+ 4 - 3
ArcToolkitCLI/Util/Glob.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.IO;
 using System.IO.Abstractions;
 
@@ -12,8 +13,8 @@ namespace ArcToolkitCLI.Util
         {
             foreach (var pattern in patterns)
                 if (IsGlob(pattern))
-                    foreach (var fileSystemInfoBase in Ganss.IO.Glob.Expand(pattern))
-                        yield return fileSystemInfoBase;
+                    foreach (var file in GlobExpressions.Glob.Files(Environment.CurrentDirectory, pattern))
+                        yield return new FileInfoWrapper(new FileSystem(), new FileInfo(file));
                 else
                     yield return new FileInfoWrapper(new FileSystem(), new FileInfo(pattern));
         }

+ 4 - 2
ArcToolkitCLI/packages.config

@@ -1,8 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
-
 <packages>
-  <package id="Glob.cs" version="3.0.27" targetFramework="net461" />
+  <package id="Glob" version="1.1.3" targetFramework="net461" />
   <package id="ILRepack" version="2.0.16" targetFramework="net461" />
   <package id="ILRepack.Lib.MSBuild.Task" version="2.0.16.1" targetFramework="net461" />
   <package id="System.IO.Abstractions" version="3.0.10" targetFramework="net461" />
+  <package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" />
+  <package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
+  <package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
 </packages>