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
73 lines
2.1 KiB
C#
73 lines
2.1 KiB
C#
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace pkNX.Containers
|
|
{
|
|
public class Mini : IFileContainer
|
|
{
|
|
public string Identifier { get; }
|
|
public byte[][] Files { get; private set; }
|
|
|
|
public Mini(byte[][] data, string ident)
|
|
{
|
|
Identifier = ident;
|
|
Files = data;
|
|
Backup = new byte[data.Length][];
|
|
for (int i = 0; i < Backup.Length; i++)
|
|
Backup[i] = (byte[])data[i].Clone();
|
|
}
|
|
|
|
public byte[] this[int index]
|
|
{
|
|
get => Files[index];
|
|
set
|
|
{
|
|
Files[index] = value;
|
|
Modified = true;
|
|
}
|
|
}
|
|
|
|
public string FilePath { get; set; }
|
|
private readonly byte[][] Backup;
|
|
|
|
public bool Modified { get; set; }
|
|
public int Count => Files.Length;
|
|
|
|
public Task<byte[]> GetFile(int file, int subFile = 0) => Task.FromResult(Files[file]);
|
|
public Task SetFile(int file, byte[] value, int subFile = 0) => Task.FromResult(Files[file] = value);
|
|
public Task<byte[][]> GetFiles() => Task.FromResult(Files);
|
|
|
|
public void CancelEdits()
|
|
{
|
|
Modified = false;
|
|
Files = new byte[Backup.Length][];
|
|
for (int i = 0; i < Files.Length; i++)
|
|
Files[i] = (byte[]) Backup[i].Clone();
|
|
}
|
|
|
|
public Task SaveAs(string path, ContainerHandler handler, CancellationToken token)
|
|
{
|
|
return new Task(() =>
|
|
{
|
|
byte[] data = MiniUtil.PackMini(Files, Identifier);
|
|
File.WriteAllBytes(path, data);
|
|
}, token);
|
|
}
|
|
|
|
public void Dump(string path, ContainerHandler handler)
|
|
{
|
|
string format = this.GetFileFormatString();
|
|
Directory.CreateDirectory(path);
|
|
|
|
handler.Initialize(Count);
|
|
for (int i = 0; i < Count; i++)
|
|
{
|
|
var fn = Path.Combine(path, i.ToString(format) + ".bin");
|
|
File.WriteAllBytes(fn, Files[i]);
|
|
handler.StepFile(i + 1);
|
|
}
|
|
}
|
|
}
|
|
}
|