1
0
Fork 0
mirror of https://github.com/oMaN-Rod/nxDumpFuse.git synced 2024-11-09 12:11:45 +00:00

Fix input files filter to account for each file case

This commit is contained in:
Omar 2021-10-28 20:01:40 -04:00
parent e431a72e7e
commit dbfff7fabe
2 changed files with 49 additions and 7 deletions

View file

@ -0,0 +1,12 @@
namespace nxDumpFuse.Model.Enums
{
public enum FileCase
{
XciNumeric, // .xci.00
NspNumeric, // .nsp.00
Xci, // .xc0
Nsp, // .ns0
Numeric // dir/00
}
}

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -17,6 +18,7 @@ namespace nxDumpFuse.Model
private readonly string _inputFilePath; private readonly string _inputFilePath;
private readonly string _outputDir; private readonly string _outputDir;
private string _outputFilePath = string.Empty; private string _outputFilePath = string.Empty;
private FileCase _fileCase;
public Fuse(string inputFilePath, string outputDir) public Fuse(string inputFilePath, string outputDir)
{ {
@ -80,8 +82,8 @@ namespace nxDumpFuse.Model
Log(FuseSimpleLogType.Error, "No input files found"); Log(FuseSimpleLogType.Error, "No input files found");
return; return;
} }
inputFiles.Sort();
FuseFiles(inputFiles, _outputFilePath); FuseFiles(inputFiles);
} }
private void GetOutputFilePath() private void GetOutputFilePath()
@ -96,23 +98,30 @@ namespace nxDumpFuse.Model
if (int.TryParse(ext, out _) && split.Count >= 3 && split[^2] == XciExt) // .xci.00 if (int.TryParse(ext, out _) && split.Count >= 3 && split[^2] == XciExt) // .xci.00
{ {
_outputFilePath = Path.Join(_outputDir, $"{string.Join("", split.Take(split.Count - 2))}.{XciExt}"); _outputFilePath = Path.Join(_outputDir, $"{string.Join("", split.Take(split.Count - 2))}.{XciExt}");
_fileCase = FileCase.XciNumeric;
} }
else if (int.TryParse(ext, out _) && split.Count >= 3 && split[^2] == NspExt) // .nsp.00 else if (int.TryParse(ext, out _) && split.Count >= 3 && split[^2] == NspExt) // .nsp.00
{
_outputFilePath = Path.Join(_outputDir, $"{string.Join("", split.Take(split.Count - 2))}.{NspExt}"); _outputFilePath = Path.Join(_outputDir, $"{string.Join("", split.Take(split.Count - 2))}.{NspExt}");
_fileCase = FileCase.NspNumeric;
}
else switch (ext[..2]) else switch (ext[..2])
{ {
// .xc0 // .xc0
case "xc" when int.TryParse(ext.Substring(ext.Length - 1, 1), out _): case "xc" when int.TryParse(ext.Substring(ext.Length - 1, 1), out _):
_outputFilePath = Path.Join(_outputDir, $"{Path.GetFileNameWithoutExtension(fileName)}.{XciExt}"); _outputFilePath = Path.Join(_outputDir, $"{Path.GetFileNameWithoutExtension(fileName)}.{XciExt}");
_fileCase = FileCase.Xci;
break; break;
// .ns0 // .ns0
case "ns" when int.TryParse(ext.Substring(ext.Length - 1, 1), out _): case "ns" when int.TryParse(ext.Substring(ext.Length - 1, 1), out _):
_outputFilePath = Path.Join(_outputDir, $"{Path.GetFileNameWithoutExtension(fileName)}.{NspExt}"); _outputFilePath = Path.Join(_outputDir, $"{Path.GetFileNameWithoutExtension(fileName)}.{NspExt}");
_fileCase = FileCase.Nsp;
break; break;
} }
} }
else // dir/00 else // dir/00
{ {
_fileCase = FileCase.Numeric;
var inputDir = new FileInfo(_inputFilePath).Directory?.Name; var inputDir = new FileInfo(_inputFilePath).Directory?.Name;
if (string.IsNullOrEmpty(inputDir)) if (string.IsNullOrEmpty(inputDir))
{ {
@ -128,16 +137,16 @@ namespace nxDumpFuse.Model
} }
} }
private async void FuseFiles(IReadOnlyCollection<string> inputFiles, string outputFilePath) private async void FuseFiles(IReadOnlyCollection<string> inputFiles)
{ {
var buffer = new byte[1024 * 1024]; var buffer = new byte[1024 * 1024];
var count = 0; var count = 0;
long totalBytes = 0; long totalBytes = 0;
var totalFileLength = GetTotalFileSize(inputFiles); var totalFileLength = GetTotalFileSize(inputFiles);
Log(FuseSimpleLogType.Information, $"Fusing {inputFiles.Count} parts to {outputFilePath} ({ToMb(totalFileLength)}MB)"); Log(FuseSimpleLogType.Information, $"Fusing {inputFiles.Count} parts to {_outputFilePath} ({ToMb(totalFileLength)}MB)");
await using var outputStream = File.Create(outputFilePath); await using var outputStream = File.Create(_outputFilePath);
foreach (var inputFilePath in inputFiles) foreach (var inputFilePath in inputFiles)
{ {
if (_cts.Token.IsCancellationRequested) return; if (_cts.Token.IsCancellationRequested) return;
@ -189,8 +198,29 @@ namespace nxDumpFuse.Model
private List<string> GetInputFiles() private List<string> GetInputFiles()
{ {
var inputDir = Path.GetDirectoryName(_inputFilePath); var inputDir = Path.GetDirectoryName(_inputFilePath);
if (string.IsNullOrEmpty(inputDir)) inputDir = Path.GetPathRoot(_inputFilePath); if (string.IsNullOrEmpty(inputDir)) return new List<string>();
return inputDir != null ? Directory.GetFiles(inputDir, $"{Path.GetFileNameWithoutExtension(_inputFilePath)}*").ToList() : new List<string>(); var files = new List<string>();
switch (_fileCase)
{
case FileCase.XciNumeric: // .xci.00
case FileCase.NspNumeric: // .nsp.00
files = Directory.GetFiles(inputDir)
.Where(f => int.TryParse(Path.GetExtension(f).Replace(".", ""), out _))
.ToList();
break;
case FileCase.Xci: // .xc0
case FileCase.Nsp: // .ns0
files = Directory.GetFiles(inputDir, $"{Path.GetFileNameWithoutExtension(_inputFilePath)}*")
.ToList();
break;
case FileCase.Numeric: // dir/00
files = Directory.GetFiles(inputDir)
.Where(f => int.TryParse(Path.GetFileName(f), out _))
.ToList();
break;
}
files.Sort();
return files;
} }
public void StopFuse() public void StopFuse()