Add r/w mitm path replacements

This commit is contained in:
Kurt
2018-11-26 21:52:45 -08:00
parent 7572f7940f
commit ed485e0643
3 changed files with 35 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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);