mirror of
https://github.com/kwsch/pkNX.git
synced 2026-07-07 12:44:29 -05:00
On saveall only clear cache at end allow cancellation of modifications -- clear cached modifications / revert to backup
35 lines
1.4 KiB
C#
35 lines
1.4 KiB
C#
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace pkNX.Containers
|
|
{
|
|
public class SingleFileContainer : IFileContainer
|
|
{
|
|
public string FilePath { get; set; }
|
|
public bool Modified { get; set; }
|
|
public int Count => 1;
|
|
|
|
public byte[] Data;
|
|
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));
|
|
|
|
private void LoadData(byte[] data) => Backup = (byte[]) (Data = data).Clone();
|
|
|
|
public void CancelEdits()
|
|
{
|
|
Modified = false;
|
|
Data = (byte[]) Backup.Clone();
|
|
}
|
|
|
|
public byte[] this[int index] { get => Data; set => Data = value; }
|
|
public Task<byte[][]> GetFiles() => new Task<byte[][]>(() => new[] {Data});
|
|
public Task<byte[]> GetFile(int file, int subFile = 0) => new Task<byte[]>(() => Data);
|
|
public Task SetFile(int file, byte[] value, int subFile = 0) => new Task(() => 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);
|
|
}
|
|
}
|