Skip to content

Commit

Permalink
Configs: Fix subdirectories not getting included (except plugins)
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Dec 18, 2023
1 parent 25cc2d8 commit 1601c14
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions UnrealVR/GameConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,53 @@ public static void CreateZipFromDirectory(string sourceDirectoryName, string des
try {
using (FileStream zipToOpen = new FileStream(destinationArchiveFileName, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create)) {
foreach (var file in Directory.GetFiles(sourceDirectoryName)) {
var entryName = Path.GetFileName(file);
var entry = archive.CreateEntry(entryName);
entry.LastWriteTime = File.GetLastWriteTime(file);
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var stream = entry.Open()) {
fs.CopyTo(stream);
}
}
DirectoryInfo di = new DirectoryInfo(sourceDirectoryName);
string basePath = di.FullName;

// Recursive method to process directories
ProcessDirectory(di, basePath, archive);
}
} catch (Exception ex) {
MessageBox.Show("An error occurred: " + ex.Message);
}
}

private static void ProcessDirectory(DirectoryInfo di, string basePath, ZipArchive archive) {
// Process files in the directory
foreach (FileInfo file in di.GetFiles()) {
try {
string entryName = GetRelativePath(file.FullName, basePath);
var entry = archive.CreateEntry(entryName);
entry.LastWriteTime = file.LastWriteTime;

using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var stream = entry.Open()) {
fs.CopyTo(stream);
}
} catch(Exception) {
continue;
}
}

// Recursively process subdirectories
foreach (DirectoryInfo subDi in di.GetDirectories()) {
if (subDi.Name.Equals("plugins", StringComparison.OrdinalIgnoreCase)) {
continue;
}

ProcessDirectory(subDi, basePath, archive);
}
}

private static string GetRelativePath(string fullPath, string basePath) {
// Ensure trailing backslash
if (!basePath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) {
basePath += Path.DirectorySeparatorChar;
}

return fullPath.Substring(basePath.Length);
}

public static void ExtractZipToDirectory(string sourceArchiveFileName, string destinationDirectoryName) {
try {
ZipFile.ExtractToDirectory(sourceArchiveFileName, destinationDirectoryName, overwriteFiles: true);
Expand Down

0 comments on commit 1601c14

Please sign in to comment.