Set up container r/w mitm

This commit is contained in:
Kurt 2018-11-26 21:36:43 -08:00
parent 2e12e326ef
commit 7572f7940f
7 changed files with 39 additions and 13 deletions

View File

@ -0,0 +1,22 @@
using System.IO;
namespace pkNX.Containers
{
public static class FileMitm
{
public static byte[] ReadAllBytes(string path)
{
return File.ReadAllBytes(path);
}
public static void WriteAllBytes(string path, byte[] data)
{
File.WriteAllBytes(path, data);
}
public static string GetRedirectedPath(string path)
{
return path;
}
}
}

View File

@ -49,7 +49,7 @@ public byte[] GetFileData(string file)
if (index < 0)
return null;
string path = Paths[index];
var data = Data[index] ?? (Data[index] = File.ReadAllBytes(path));
var data = Data[index] ?? (Data[index] = FileMitm.ReadAllBytes(path));
return (byte[])data.Clone();
}
@ -57,7 +57,7 @@ public byte[] GetFileData(int index)
{
if (index < 0 || (uint)index >= Data.Count)
return null;
var data = Data[index] ?? (Data[index] = File.ReadAllBytes(Paths[index]));
var data = Data[index] ?? (Data[index] = FileMitm.ReadAllBytes(Paths[index]));
return (byte[])data.Clone();
}
@ -84,7 +84,7 @@ public bool Modified
public int Count => Paths.Count;
public Task<byte[][]> GetFiles() => Task.FromResult(Paths.Select(File.ReadAllBytes).ToArray());
public Task<byte[][]> GetFiles() => Task.FromResult(Paths.Select(FileMitm.ReadAllBytes).ToArray());
public Task<byte[]> GetFile(int file, int subFile = 0) => Task.FromResult(this[file]);
public Task SetFile(int file, byte[] value, int subFile = 0) => Task.FromResult(this[file] = value);
public Task SaveAs(string path, ContainerHandler handler, CancellationToken token) => new Task(SaveAll, token);
@ -98,7 +98,7 @@ private void SaveAll()
var data = Data[i];
if (data == null)
continue;
File.WriteAllBytes(Paths[i], data);
FileMitm.WriteAllBytes(Paths[i], data);
}
}

View File

@ -41,6 +41,7 @@ public abstract class LargeContainer : IDisposable, IFileContainer
protected void OpenBinary(string path)
{
path = FileMitm.GetRedirectedPath(path);
Stream = new FileStream(path, FileMode.Open);
Reader = new BinaryReader(Stream);
Initialize();
@ -117,6 +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);
var stream = new FileStream(path, FileMode.CreateNew);
using (var bw = new BinaryWriter(stream))
await Pack(bw, handler, token).ConfigureAwait(false);

View File

@ -52,7 +52,7 @@ public Task SaveAs(string path, ContainerHandler handler, CancellationToken toke
return new Task(() =>
{
byte[] data = MiniUtil.PackMini(Files, Identifier);
File.WriteAllBytes(path, data);
FileMitm.WriteAllBytes(path, data);
}, token);
}
@ -65,7 +65,7 @@ public void Dump(string path, ContainerHandler handler)
for (int i = 0; i < Count; i++)
{
var fn = Path.Combine(path, i.ToString(format) + ".bin");
File.WriteAllBytes(fn, Files[i]);
FileMitm.WriteAllBytes(fn, Files[i]);
handler.StepFile(i + 1);
}
}

View File

@ -14,7 +14,7 @@ public static byte[] PackMini(string[] files, string identifier)
{
byte[][] fileData = new byte[files.Length][];
for (int i = 0; i < fileData.Length; i++)
fileData[i] = File.ReadAllBytes(files[i]);
fileData[i] = FileMitm.ReadAllBytes(files[i]);
return PackMini(fileData, identifier);
}
@ -66,7 +66,7 @@ public static byte[] PackMini(byte[][] fileData, string identifier)
public static byte[][] UnpackMini(string file, string identifier = null)
{
byte[] fileData = File.ReadAllBytes(file);
byte[] fileData = FileMitm.ReadAllBytes(file);
return UnpackMini(fileData, identifier);
}
@ -98,6 +98,7 @@ public static byte[][] UnpackMini(byte[] fileData, string identifier = null)
public static Mini GetMini(string path)
{
path = FileMitm.GetRedirectedPath(path);
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var br = new BinaryReader(fs))
return GetMini(br);
@ -137,6 +138,7 @@ public static string GetIsMini(string path)
{
try
{
path = FileMitm.GetRedirectedPath(path);
using (var fs = new FileStream(path, FileMode.Open))
using (var br = new BinaryReader(fs))
return GetIsMini(br);

View File

@ -22,7 +22,7 @@ public class GFPack : IEnumerable<byte[]>, IFileContainer
public byte[][] CompressedFiles { get; set; }
public byte[][] DecompressedFiles { get; set; }
public GFPack(string path) : this(File.ReadAllBytes(path)) => FilePath = path;
public GFPack(string path) : this(FileMitm.ReadAllBytes(path)) => FilePath = path;
public GFPack(string[] directories, string parent = @"\bin") => LoadFiles(directories, parent);
/// <summary>
@ -195,7 +195,7 @@ public void LoadFiles(string[] directories, string parent, CompressionType type
HashPaths[i] = new FileHashPath {HashFnv1aPathFull = hashFull};
HashIndexes[i] = new FileHashIndex {HashFnv1aPathFileName = hashShort, Index = i};
FileTable[i] = new FileData {Type = type};
DecompressedFiles[i] = File.ReadAllBytes(file);
DecompressedFiles[i] = FileMitm.ReadAllBytes(file);
}
Modified = true;
}
@ -288,7 +288,7 @@ public void Dump(string path, ContainerHandler handler)
var fn = $"{i.ToString(format)} {hashFull:X16}.bin";
var loc = Path.Combine(path ?? FilePath, fn);
var data = DecompressedFiles[i];
File.WriteAllBytes(loc, data);
FileMitm.WriteAllBytes(loc, data);
handler.StepFile(i, fileName: fn);
}
}

View File

@ -15,7 +15,7 @@ public class SingleFileContainer : IFileContainer
private byte[] Backup;
public SingleFileContainer(byte[] data) => LoadData(data);
public SingleFileContainer(BinaryReader br) => LoadData(br.ReadBytes((int) br.BaseStream.Length));
public SingleFileContainer(string path) => LoadData(File.ReadAllBytes(FilePath = path));
public SingleFileContainer(string path) => LoadData(FileMitm.ReadAllBytes(FilePath = path));
private void LoadData(byte[] data) => Backup = (byte[]) (Data = data).Clone();
@ -39,6 +39,6 @@ public void CancelEdits()
public Task<byte[]> GetFile(int file, int subFile = 0) => Task.FromResult(this[0]);
public Task SetFile(int file, byte[] value, int subFile = 0) => Task.FromResult(Data = value);
public Task SaveAs(string path, ContainerHandler handler, CancellationToken token) => new Task(() => Dump(path, handler), token);
public void Dump(string path, ContainerHandler handler) => File.WriteAllBytes(path ?? FilePath, Data);
public void Dump(string path, ContainerHandler handler) => FileMitm.WriteAllBytes(path ?? FilePath, Data);
}
}