Skip to content

Commit

Permalink
ditto
Browse files Browse the repository at this point in the history
  • Loading branch information
th3w1zard1 committed May 22, 2023
1 parent 762d2ab commit d622a81
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 40 deletions.
42 changes: 27 additions & 15 deletions KOTORModSync.Core/Utility/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,28 +373,37 @@ public static async Task<List<string>> EnumerateFilesWithWildcards(string direct
{
var matchingFiles = new List<string>();

string regexPattern = "^" + Regex.Escape(pattern)
.Replace("\\*", ".*")
.Replace("\\?", ".")
+ "$";
string directoryPath = Path.GetDirectoryName(directory);
string directoryNamePattern = Path.GetFileName(directory);

IEnumerable<string> files = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories);
foreach (string file in files)
IEnumerable<string> matchingDirectories = Directory.GetDirectories(directoryPath, directoryNamePattern, SearchOption.TopDirectoryOnly);

foreach (string matchingDirectory in matchingDirectories)
{
if (Regex.IsMatch(Path.GetFileName(file), regexPattern))
string filePattern = Path.GetFileName(pattern);
string regexPattern = "^" + Regex.Escape(filePattern)
.Replace("\\*", ".*")
.Replace("\\?", ".")
+ "$";

IEnumerable<string> files = Directory.EnumerateFiles(matchingDirectory, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
matchingFiles.Add(file);
if (Regex.IsMatch(Path.GetFileName(file), regexPattern))
{
matchingFiles.Add(file);
}
}
}

if (!excludeDirectories)
{
IEnumerable<string> directories = Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories);
foreach (string subdirectory in directories)
if (!excludeDirectories)
{
if (Regex.IsMatch(Path.GetFileName(subdirectory), regexPattern))
IEnumerable<string> directories = Directory.EnumerateDirectories(matchingDirectory, "*", SearchOption.AllDirectories);
foreach (string subdirectory in directories)
{
matchingFiles.Add(subdirectory);
if (Regex.IsMatch(Path.GetFileName(subdirectory), regexPattern))
{
matchingFiles.Add(subdirectory);
}
}
}
}
Expand All @@ -404,6 +413,9 @@ public static async Task<List<string>> EnumerateFilesWithWildcards(string direct






public static List<Component> ReadComponentsFromFile(string filePath)
{
try
Expand Down
71 changes: 47 additions & 24 deletions KOTORModSync.Core/installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,23 @@ public async Task<bool> ExtractFileAsync()
try
{
(List<string> sourcePaths, DirectoryInfo _) = await ParsePathsAsync();
List<Task> extractionTasks = new List<Task>();

// Use SemaphoreSlim to limit concurrent extractions
SemaphoreSlim semaphore = new SemaphoreSlim(5); // Limiting to 5 concurrent extractions

foreach (string sourcePath in sourcePaths)
{
var thisFile = new FileInfo(sourcePath);
Logger.Log($"File path: {thisFile.FullName}");
await semaphore.WaitAsync(); // Acquire a semaphore slot

if (ArchiveHelper.IsArchive(thisFile.Extension))
extractionTasks.Add(Task.Run(async () =>
{
List<ArchiveEntry> archiveEntries = ArchiveHelper.TraverseArchiveEntries(thisFile.FullName);

foreach (ArchiveEntry entry in archiveEntries)
try
{
string destinationFolder = Path.GetFileNameWithoutExtension(thisFile.Name);
string destinationPath = Path.Combine(thisFile.Directory.FullName, destinationFolder, entry.Path);
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
using (Stream outputStream = File.Create(destinationPath))
var thisFile = new FileInfo(sourcePath);
Logger.Log($"File path: {thisFile.FullName}");
if (ArchiveHelper.IsArchive(thisFile.Extension))
{
using (FileStream stream = File.OpenRead(thisFile.FullName))
{
Expand All @@ -131,31 +133,50 @@ public async Task<bool> ExtractFileAsync()
{
archive = SevenZipArchive.Open(stream);
}
Logger.Log($"Attempting to extract {thisFile.Name}");
if (archive != null)
{
IArchiveEntry archiveEntry = archive.Entries.FirstOrDefault(e => e.Key == entry.Path);
if (archiveEntry != null && !archiveEntry.IsDirectory)
List<ArchiveEntry> archiveEntries = ArchiveHelper.TraverseArchiveEntries(thisFile.FullName);
// Extract entries in parallel
await Task.WhenAll(archiveEntries.Select(async entry =>
{
Logger.Log($"Extracting {archiveEntry.Key}");
using (Stream entryStream = archiveEntry.OpenEntryStream())
string destinationFolder = Path.GetFileNameWithoutExtension(thisFile.Name);
string destinationPath = Path.Combine(thisFile.Directory.FullName, destinationFolder, entry.Path);
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
using (Stream outputStream = File.Create(destinationPath))
{
entryStream.CopyTo(outputStream);
IArchiveEntry archiveEntry = archive.Entries.FirstOrDefault(e => e.Key == entry.Path);
if (archiveEntry != null && !archiveEntry.IsDirectory)
{
Logger.Log($"Extracting {archiveEntry.Key}");
using (Stream entryStream = archiveEntry.OpenEntryStream())
{
await entryStream.CopyToAsync(outputStream);
}
}
}
}
}));
}
}
}
else
{
var ex = new ArgumentNullException($"{this.ParentComponent.Name} failed to extract file {thisFile}");
Logger.LogException(ex);
throw ex;
}
}
}
else
{
var ex = new ArgumentNullException($"{this.ParentComponent.Name} failed to extract file {thisFile}");
Logger.LogException(ex);
throw ex;
}
finally
{
semaphore.Release(); // Release the semaphore slot
}
}));
}

await Task.WhenAll(extractionTasks); // Wait for all extraction tasks to complete

return true; // Extraction succeeded
}
catch (Exception ex)
Expand All @@ -166,6 +187,8 @@ public async Task<bool> ExtractFileAsync()
}
}



public async Task<bool> DeleteFileAsync()
{
try
Expand Down
2 changes: 1 addition & 1 deletion KOTORModSync.GUI/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private bool CheckForChanges()

private async void SaveButton_Click(object sender, RoutedEventArgs e)
{ if (currentComponent is null)
currentComponent = leftTreeView.SelectedItem as Core.Component;
currentComponent = leftTreeView.SelectedItem as Component;
if (currentComponent is null)
{
var informationDialog = new InformationDialog();
Expand Down

0 comments on commit d622a81

Please sign in to comment.