pkNX/pkNX.Containers/InternalFileContainer.cs
Kurt 38b784e19b Add handling for S/V load and data ripping
Co-Authored-By: SciresM <8676005+SciresM@users.noreply.github.com>
Co-Authored-By: Matt <17801814+sora10pls@users.noreply.github.com>
Co-Authored-By: Lusamine <30205550+Lusamine@users.noreply.github.com>
2022-11-24 15:30:03 -08:00

42 lines
1.3 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace pkNX.Containers;
public class InternalFileContainer : IFileContainer
{
public string? FilePath { get; set; }
public bool Modified { get; set; }
public int Count => 1;
public byte[] Data = Array.Empty<byte>();
private byte[] Backup = Array.Empty<byte>();
public InternalFileContainer(byte[] data) => LoadData(data);
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 => (byte[])Data.Clone();
set
{
Modified |= !Data.SequenceEqual(value);
Data = value;
}
}
public Task<byte[][]> GetFiles() => Task.FromResult(new[] {this[0]});
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(() => Dump(path, handler), token);
public void Dump(string? path, ContainerHandler handler) => FileMitm.WriteAllBytes(path ?? FilePath!, Data);
}