diff --git a/pkNX.Containers/FileMitm.cs b/pkNX.Containers/FileMitm.cs index cb8a6478..64d0385b 100644 --- a/pkNX.Containers/FileMitm.cs +++ b/pkNX.Containers/FileMitm.cs @@ -6,17 +6,46 @@ public static class FileMitm { public static byte[] ReadAllBytes(string path) { + path = GetRedirectedReadPath(path); return File.ReadAllBytes(path); } public static void WriteAllBytes(string path, byte[] data) { + path = GetRedirectedWritePath(path); File.WriteAllBytes(path, data); } - public static string GetRedirectedPath(string path) + public static string GetRedirectedReadPath(string path) { - return path; + if (!Enabled) + return path; + var newDest = path.Replace(PathOriginal, PathRedirect); + if (!File.Exists(newDest)) + return path; + return newDest; } + + public static string GetRedirectedWritePath(string path) + { + if (!Enabled) + return path; + var newDest = path.Replace(PathOriginal, PathRedirect); + var parent = Path.GetDirectoryName(newDest); + Directory.CreateDirectory(parent); + return newDest; + } + + public static void SetRedirect(string original, string dest) + { + PathOriginal = original; + PathRedirect = dest; + Enabled = true; + } + + public static bool Enabled { get; set; } + + private static string PathOriginal; + private static string PathRedirect; } } \ No newline at end of file diff --git a/pkNX.Containers/LargeContainer.cs b/pkNX.Containers/LargeContainer.cs index f91a9695..c2cbf8a6 100644 --- a/pkNX.Containers/LargeContainer.cs +++ b/pkNX.Containers/LargeContainer.cs @@ -41,7 +41,7 @@ public abstract class LargeContainer : IDisposable, IFileContainer protected void OpenBinary(string path) { - path = FileMitm.GetRedirectedPath(path); + path = FileMitm.GetRedirectedReadPath(path); Stream = new FileStream(path, FileMode.Open); Reader = new BinaryReader(Stream); Initialize(); @@ -118,7 +118,7 @@ public async Task SaveAs(string path, ContainerHandler handler, CancellationToke bool sameLocation = path == FilePath && Reader != null; var writePath = sameLocation ? Path.GetTempFileName() : path; - path = FileMitm.GetRedirectedPath(path); + path = FileMitm.GetRedirectedWritePath(path); var stream = new FileStream(path, FileMode.CreateNew); using (var bw = new BinaryWriter(stream)) await Pack(bw, handler, token).ConfigureAwait(false); diff --git a/pkNX.Containers/Mini/MiniUtil.cs b/pkNX.Containers/Mini/MiniUtil.cs index fee57eda..75ce7f5f 100644 --- a/pkNX.Containers/Mini/MiniUtil.cs +++ b/pkNX.Containers/Mini/MiniUtil.cs @@ -98,7 +98,7 @@ public static byte[][] UnpackMini(byte[] fileData, string identifier = null) public static Mini GetMini(string path) { - path = FileMitm.GetRedirectedPath(path); + path = FileMitm.GetRedirectedReadPath(path); using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) using (var br = new BinaryReader(fs)) return GetMini(br); @@ -138,7 +138,7 @@ public static string GetIsMini(string path) { try { - path = FileMitm.GetRedirectedPath(path); + path = FileMitm.GetRedirectedWritePath(path); using (var fs = new FileStream(path, FileMode.Open)) using (var br = new BinaryReader(fs)) return GetIsMini(br);