2021-10-08 22:38:49 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Toolbox;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Toolbox.Library;
|
|
|
|
|
using Toolbox.Library.IO;
|
2021-10-18 01:45:37 +02:00
|
|
|
|
using Toolbox.Library.Security.Cryptography;
|
2021-10-20 01:44:06 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2021-10-08 22:38:49 +02:00
|
|
|
|
|
|
|
|
|
namespace FirstPlugin
|
|
|
|
|
{
|
2021-11-04 02:03:43 +01:00
|
|
|
|
public class PKG : IArchiveFile, IFileFormat, ILeaveOpenOnLoad, ISaveOpenedFileStream
|
2021-10-08 22:38:49 +02:00
|
|
|
|
{
|
|
|
|
|
public FileType FileType { get; set; } = FileType.Archive;
|
|
|
|
|
|
2021-11-04 02:03:43 +01:00
|
|
|
|
public bool CanSave { get; set; } = true;
|
2021-10-08 22:38:49 +02:00
|
|
|
|
public string[] Description { get; set; } = new string[] { "PKG" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.pkg" };
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
public IFileInfo IFileInfo { get; set; }
|
|
|
|
|
|
2021-10-13 01:51:48 +02:00
|
|
|
|
public bool CanAddFiles { get; set; } = true;
|
|
|
|
|
public bool CanRenameFiles { get; set; } = true;
|
|
|
|
|
public bool CanReplaceFiles { get; set; } = true;
|
|
|
|
|
public bool CanDeleteFiles { get; set; } = true;
|
2021-10-08 22:38:49 +02:00
|
|
|
|
|
|
|
|
|
public bool Identify(System.IO.Stream stream)
|
|
|
|
|
{
|
|
|
|
|
return FileName.EndsWith(".pkg");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Type[] Types
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<Type> types = new List<Type>();
|
|
|
|
|
return types.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FileEntry> files = new List<FileEntry>();
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ArchiveFileInfo> Files => files;
|
|
|
|
|
|
|
|
|
|
public void ClearFiles() { files.Clear(); }
|
|
|
|
|
|
2021-10-18 01:45:37 +02:00
|
|
|
|
static Dictionary<ulong, string> HashList = new Dictionary<ulong, string>();
|
|
|
|
|
|
|
|
|
|
static void CalculateHashes()
|
|
|
|
|
{
|
|
|
|
|
var mem = new MemoryStream(Properties.Resources.MetroidDread);
|
2021-10-20 01:44:06 +02:00
|
|
|
|
using (var reader = new StreamReader(mem)) {
|
|
|
|
|
//Thanks to UltiNaruto for the hash list
|
|
|
|
|
HashList = JsonConvert.DeserializeObject<Dictionary<ulong, string>>(reader.ReadToEnd());
|
2021-10-18 01:45:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 22:38:49 +02:00
|
|
|
|
private System.IO.Stream _stream;
|
|
|
|
|
public void Load(System.IO.Stream stream)
|
|
|
|
|
{
|
2021-10-18 01:45:37 +02:00
|
|
|
|
if (HashList.Count == 0)
|
|
|
|
|
CalculateHashes();
|
|
|
|
|
|
2021-10-08 22:38:49 +02:00
|
|
|
|
_stream = stream;
|
|
|
|
|
using (var reader = new FileReader(stream, true))
|
|
|
|
|
{
|
|
|
|
|
reader.SetByteOrder(false);
|
|
|
|
|
uint headerSize = reader.ReadUInt32();
|
|
|
|
|
uint fileSize = reader.ReadUInt32();
|
|
|
|
|
uint numFiles = reader.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numFiles; i++)
|
|
|
|
|
{
|
|
|
|
|
var file = new FileEntry();
|
|
|
|
|
ulong nameHash = reader.ReadUInt64();
|
|
|
|
|
uint fileStartOffset = reader.ReadUInt32();
|
|
|
|
|
uint fileEndOffset = reader.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
uint size = fileEndOffset - fileStartOffset;
|
|
|
|
|
|
2021-11-04 02:03:43 +01:00
|
|
|
|
file.Hash = nameHash;
|
2021-10-20 01:44:06 +02:00
|
|
|
|
file.FileName = nameHash.ToString("X");
|
2021-10-08 22:38:49 +02:00
|
|
|
|
file.FileDataStream = new SubStream(reader.BaseStream,
|
|
|
|
|
fileStartOffset, size);
|
|
|
|
|
|
2021-10-18 01:45:37 +02:00
|
|
|
|
if (HashList.ContainsKey(nameHash))
|
|
|
|
|
file.FileName = HashList[nameHash];
|
2021-10-10 20:06:08 +02:00
|
|
|
|
|
2021-10-08 22:38:49 +02:00
|
|
|
|
files.Add(file);
|
|
|
|
|
}
|
2021-10-18 02:49:28 +02:00
|
|
|
|
files = files.OrderBy(x => x.FileName).ToList();
|
2021-10-08 22:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unload()
|
|
|
|
|
{
|
2021-10-13 01:51:48 +02:00
|
|
|
|
_stream?.Dispose();
|
2021-10-08 22:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save(System.IO.Stream stream)
|
|
|
|
|
{
|
2021-10-13 01:51:48 +02:00
|
|
|
|
using (var writer = new FileWriter(stream))
|
|
|
|
|
{
|
|
|
|
|
writer.Write(uint.MaxValue); //header size
|
|
|
|
|
writer.Write(uint.MaxValue); //file size
|
|
|
|
|
writer.Write(files.Count);
|
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
|
|
|
|
{
|
2021-11-04 02:03:43 +01:00
|
|
|
|
writer.Write(files[i].Hash);
|
2021-10-13 01:51:48 +02:00
|
|
|
|
writer.Write(uint.MaxValue); //start offset
|
|
|
|
|
writer.Write(uint.MaxValue); //end offset
|
|
|
|
|
}
|
|
|
|
|
writer.Align(128);
|
|
|
|
|
|
|
|
|
|
writer.WriteUint32Offset(0, 4); //Size of header - 4
|
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteUint32Offset(20 + (i * 16)); //start offset
|
|
|
|
|
files[i].FileDataStream.CopyTo(writer.BaseStream);
|
|
|
|
|
writer.WriteUint32Offset(24 + (i * 16)); //end offset
|
2021-11-04 02:03:43 +01:00
|
|
|
|
|
|
|
|
|
writer.Align(8);
|
2021-10-13 01:51:48 +02:00
|
|
|
|
}
|
2021-11-04 02:03:43 +01:00
|
|
|
|
using (writer.TemporarySeek(4, SeekOrigin.Begin)) {
|
2021-10-13 01:51:48 +02:00
|
|
|
|
writer.Write((int)writer.BaseStream.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-08 22:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
2021-11-04 02:03:43 +01:00
|
|
|
|
var hash = Crc64.Compute(archiveFileInfo.FileName);
|
|
|
|
|
|
2021-10-13 01:51:48 +02:00
|
|
|
|
files.Add(new FileEntry()
|
|
|
|
|
{
|
2021-11-04 02:03:43 +01:00
|
|
|
|
Hash = hash,
|
2021-10-13 01:51:48 +02:00
|
|
|
|
FileDataStream = archiveFileInfo.FileDataStream,
|
|
|
|
|
FileName = archiveFileInfo.FileName,
|
|
|
|
|
});
|
|
|
|
|
return true;
|
2021-10-08 22:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
2021-10-13 01:51:48 +02:00
|
|
|
|
files.Remove((FileEntry)archiveFileInfo);
|
|
|
|
|
return true;
|
2021-10-08 22:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FileEntry : ArchiveFileInfo
|
|
|
|
|
{
|
2021-11-04 02:03:43 +01:00
|
|
|
|
public ulong Hash { get; set; }
|
|
|
|
|
|
|
|
|
|
public override string FileName
|
|
|
|
|
{
|
|
|
|
|
get => base.FileName;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (base.FileName != value) {
|
|
|
|
|
//Filename changed (not empty previously)
|
|
|
|
|
bool isRenamed = !string.IsNullOrEmpty(base.FileName);
|
|
|
|
|
base.FileName = value;
|
|
|
|
|
if (isRenamed)
|
|
|
|
|
Hash = Crc64.Compute(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-08 22:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|